42.MySQL database installation, and driver selection

MySQL driver installation:

We use Django to operate Mysql, in fact, the underlying or operated by Python, so we want to use Django to operate mysql, above all, need to install a driver in Python3, the driver has a variety of options. For example, there pymysql mysqlclient and so on. Here we are to operate with pymysql. pymysql arrangement is very simple, only need to install pip install pymysql.
Common Mysql Driver Introduction:
  1. MySQL-python: i.e. MySQLdb, package is a simple operation for the C language MySQL database. Follow the Python DB API v2. But only support Python2, currently does not support Python3.
  2. mysqlclient: mysql-python is in another branch. Python3 support and fixes a bug.
  3. pymysql: to achieve a pure Python drive, because it is pure implemented in Python, thus performing efficient as MySQL-python. And also because it is written in pure Python, and Python code can be seamlessly connected.
  4. MySQL Connectot / Python: MySQL official launch of the MySQL connection using pure Python drive, because it is pure Python development, efficiency is not high.
There are two ways, the first way is to use native sql statement operations in Django database operation, the second is to use an ORM model to operate.
Using native sql statement in Django operation is actually using python db api interface to operate, if your mysql driver using pymysql, then you are using pymysql to operate, but Django database connection part of a good package, we just need to find a database configured in settings.py directly packaged Django interfaces can operate after the connection information, the following sample code:

The above method is to execute and fetchall Python DB API specification defined, any driver to use Python to operate the mysql should follow this. So whether it is or is using pymysql mysqlclient or mysqldb, their interfaces are the same.

Python DB API Specification cursor object under common interface:

  1. description: if the cursor query code is executed sql, cursor.description attribute is read when returns a list of which is encapsulated in the tuple in a tuple are encapsulated (name, type_code, display_size, internal_size , precision, scale, null_ok), which represents the parameter name to check out the data field names, other parameters being useful.
  2. rowcount: 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, whether the person will throw an exception.
  4. Execute (sql [, parameters]): execute a sql statement, if it is required to pass parameters in the implementation of the sql statement, then the parameters can be passed parameters, the following sample code:
  5. fetchone: obtaining the first data after performing a query operation.
  6. fetchmany (size): After re-execute the query operation, a plurality of data acquisition, the specific number depending on the size of the size, if the size parameter is not passed, then the default acquiring first data.
  7. fetchall: Get all meet sql statement data.
Published 75 original articles · won praise 2 · Views 2735

Guess you like

Origin blog.csdn.net/zjy123078_zjy/article/details/104023414