PythonDjango connects to the MySQL database for addition, deletion, modification, and query

Source: Internet
Author: User
This article describes how to connect pythonDjango to the MySQL database and provides code for addition, deletion, modification, and query. 1. download and install the MySQLdb class library
Http://www.djangoproject.com/r/python-mysql/
2. modify settings. py to configure data attributes.

The code is as follows:


DATABASES = {
'Default ':{
'Engine ': 'Django. db. backends. mysql', # Add 'postgresql _ psycopg2', 'mysqlite3 ', or 'oracle '.
'Name': 'djangdb', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'User': 'root ',
'Password': 'root ',
'Host': '2017. 0.0.1 ', # Empty for localhost through domain sockets or '2017. 0.0.1' for localhost through TCP.
'Port': '123', # Set to empty string for default.
}
}


After the modification, enter DOS and run the python manage. py shell command in the project directory to start the interaction interface. enter the code to verify whether the database configuration is successful. If no error is reported, the operation is successful!

The code is as follows:


>>> From django. db import connection
>>> Cursor = connection. cursor ()


3. create a Django app
A project contains one or more such apps. An app can be understood as a collection of functions. For example, the product management module includes adding, deleting, and querying functions. you can call product management an app. Each Django app has its own models and views, which are easy to transplant and reuse.
DOS enters the project directory and runs python manage. py startapp products to generate the directory file as follows:

The code is as follows:


Products/
_ Init _. py
Models. py
Tests. py
Views. py


4. compile models

The code is as follows:


From django. db import models
# Create your models here.
Class Company (models. Model ):
Full_name = models. CharField (max_length = 30)
Address = models. CharField (max_length = 50)
Tel = models. CharField (max_length = 15, blank = True)
Class Product (models. Model ):
Product_name = models. CharField (max_length = 30)
Price = models. FloatField ()
Stock = models. IntegerField (max_length = 5)
Company = models. ForeignKey (Company)


5. model installation (modify settings. py)

The code is as follows:


INSTALLED_APPS = (
'Django. contrib. auth ',
'Django. contrib. contenttypes ',
'Django. contrib. session ',
'Django. contrib. sites ',
'Django. contrib. messages ',
'Django. contrib. staticfiles ',
# Uncomment the next line to enable the admin:
'Django. contrib. admin ',
# Uncomment the next line to enable admin documentation:
'Django. contrib. admindocs ',
'Djangomysqlsite. products ',
)



Use python manage. py validate to check whether the syntax and logic of the model are correct.
If no error occurs, run python manage. py syncdb to create a data table.
Now you can see that in addition to generating products_company and products_product, your database also creates several other tables, which are required by the django Management backend.

6. simple addition, deletion, modification, and query
Go to python manage. py shell

The code is as follows:


From DjangoMysqlSite. products. models import Company
>>> C = Company (full_name = 'group', address = 'Hangzhou Xihu ', tel = 8889989)
>>> C. save ()

>>> Company_list = Company. objects. all ()
>>> Company_list

>>> C = Company. objects. get (full_name = "group ")
>>> C. tel = 123456
>>> C. save ()

>>> C = Company. objects. get (full_name = "group ")
>>> C. delete ()
# Delete all
>>> Company. objects. all (). delete ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.