11. database operations

 

MySQL driver installation:

We used to operate Django MySQL, the underlying fact or operated by Python. So we want to use Django to operate MySQL, above all, you need to install a driver. In Python3, the driver has a variety of options. For example, there pymysql mysqlclient and so on. Here we use mysqlclientto operate. mysqlclientInstallation is very simple. Only you need pip install mysqlclientto install.

MySQL introduced a common drive:

  1. MySQL-python: That is MySQLdb. It is a C language simple package MySQL database operation. Follow the Python DB API v2. But only to support Python2, currently does not support Python3.
  2. mysqlclient: Is another branch of MySQL-python. Python3 support and fixes some bug.
  3. pymysql: Drive a pure Python implementation. Because it is written in pure Python, and therefore the implementation of efficient than MySQL-python. And also because it is written in pure Python, and Python code can be seamless.
  4. MySQL Connector/Python: MySQL official launch of the drive using pure Python connection MySQL. Because it is pure Python development. low efficiency.

Django configuration database connection:

Before operating the database, you first must first connect to the database. Here we have an example to explain to configure MySQL. Django database connection, do not need to create a separate connection object. Only you need to settings.pydo a good job database-related configuration files on it. Sample code is as follows:

DATABASES = {
    'default': {
        # 数据库引擎(是mysql还是oracle等)
        'ENGINE': 'django.db.backends.mysql',
        # 数据库的名字
        'NAME': 'dfz',
        # 连接mysql数据库的用户名
        'USER': 'root',
        # 连接mysql数据库的密码
        'PASSWORD': 'root',
        # mysql数据库的主机地址
        'HOST': '127.0.0.1',
        # mysql数据库的端口号
        'PORT': '3306',
    }
}

In Django operation of the database:

In Django database operation in two ways. The first way is to use native operating sql statement, the second is to use an ORM model to operate. This lesson first of all in terms of one kind Dir.

Using native sql statement in Django operation is actually using python db api interface to operate. If you are using the mysql driven pymysql, then you are using pymysql to operate, but this will be part of the package Django database connections Well, as long as we configured in settings.py database connection information used directly after Django package good Interface can operate up. Sample code is as follows:

# 使用django封装好的connection对象,会自动读取settings.py中数据库的配置信息
from django.db import connection

# 获取游标对象
cursor = connection.cursor()
# 拿到游标对象后执行sql语句
cursor.execute("select * from book")
# 获取所有的数据
rows = cursor.fetchall()
# 遍历查询到的数据
for row in rows:
    print(row)


Fetchall above and execute Python DB API methods are defined in the specification. Any driver using Python to manipulate the MySQL should follow this. So whether it is or is using pymysql mysqlclient or mysqldb, their interfaces are the same. For more specifications please refer to: https://www.python.org/dev/peps/pep-0249/ .

Common Interface Specification under the cursor object API Python DB:

  1. description: If the cursor query is executed sql code. So read the cursor.descriptionproperty when returns a list, which is loaded with tuples, tuples are installed respectively (name,type_code,display_size,internal_size,precision,scale,null_ok), where name represents the name of the field to find out the data, not the other parameters being useful.

  2. rowcount: It represents the number of rows after executing a sql statement affected.

  3. close: Close the cursor. After closing the cursor can no longer be used, otherwise it will throw an exception.

  4. execute(sql[,parameters]): Execute a sql statement. If you need to pass parameters in the implementation of sql statement, you can pass parameters parameters. Sample code is as follows:

     cursor.execute("select * from article where id=%s",(1,))
    
  5. fetchone: After performing a query, obtaining the first data.

  6. fetchmany(size): After performing the search operation, acquire pieces of data. Specifically how many depends on the size parameter pass. If you do not pass the size parameter, then the default is to obtain the first data.

  7. fetchall: Get all meet sql statement data.

Guess you like

Origin www.cnblogs.com/ys-python/p/11266144.html