Django database library uses detailed MySQLdb

Django project to operate the database, you must first establish a connection and database in order to make the program and database data in association with data additions and deletions to change search operation

Django project using mysqldb module and interoperate between mysql database by default.

Django database look following operation steps:

1. Modify configuration data items settings.py

 

 

 

Configuration item parameters Usage:

ENGINE: Specifies the database driver, different databases in this field, the following is a common centralized database ENGINE wording:

django.db.backends.postgresql  # PostgreSQL  

django.db.backends.mysql       # mysql  

django.db.backends.sqlite3     # sqlite  

django.db.backends.oracle      # oracle  

NAME: Specifies the database name, and if sqlite, then you need to fill the database file absolute position

USER: User name database login, mysql usually root

PASSWORD: password database, the user must be a corresponding password USER

HOST: As the general database are C / S structure, so you have to specify the database server location, we generally database server and client are in a host above, it is generally the default will fill 127.0.0.1

PORT: database server port, mysql default is 3306

HOST and PORT can not fill, use the default configuration, but if you have to change the default configuration, then you need to fill in after the change

2. Install the database driver

This configuration finished, the following will need to install the driver python mysql database connection, first of all, you need to install mysql development kits, running shell, inside:                                                                                                              If not, then the installation:                                                                                                                                                                                            After installation, start django                                                                                          

 

 If no error appears, the normal configuration, and then run in a new shell:

Enter the following command:

 

 

 

 If there is no error, then it indicates that the installation was successful, you can enjoy the use of the database functionality django

3. Create a Django app

A project contains one or more such app. app can be understood as a set of functions. Such as product management module contains additions and deletions to the search and other functions, it can be called a product management app. Each has a separate Django app models, views, etc., are multiplexed and easy to transplant.

DOS directory into the project execution python manage.py startapp products generated catalog file is as follows:

code show as below:

4. Write models

code show as below:

5. Installation model (the settings.py modified)

code show as below: 

Check the model using python manage.py validate the syntax and logic are correct.

No error is performed python manage.py syncdb create a data table.

Now you can see your database in addition to generating a products_company, outside products_product also created several other tables, these tables are required regardless of temporary django management background.

6, simple CRUD

Enter python manage.py shell

code show as below:

 

Guess you like

Origin www.cnblogs.com/programmer123/p/11729815.html