Python study notes (XV) - database operations

We write code at work, they often come to operate the database, here explain how to operate python mysql database.

python3 operation mysql database need to install a third-party modules, pymysql; python2 is MySQLdb module, the module is not in python3 the MySQLdb, so use pymysql.

A, pymysql installation

The installation of third-party modules
1, Fool installation, use Python comes pip command to install
2, manually install the
  installation at the end of .tar:
    1, first unzip the file, unzip into this directory
    2, execute: Python setup. py install installed
  mounting end .whl of:
    directly pip install C: \ XX \ XXX \ XXX.whl file (absolute path / paths relative)
Note: when a plurality of versions of the computer, when installing the module, can be Python uses PythonX.X -m -pip install XXX way to install the files to the specified

Details Reference: https://www.cnblogs.com/beginner-boy/p/7247688.html

Second, the basic operation of the database - add, delete, change,

1, Python MySQL basic steps of operation:

  1, establish a database connection

  2, () to create a cursor object via cursor (pymysql sql to perform and get results through a cursor)

  3, using the execute (), execute SQL statements

  4, to obtain the result (query) / commit the transaction (add, delete, change)

  5. Close the cursor

  6, close the connection

Note: perform CRUD operations with Python before, after good idea to check under the sql statement is correct, make sure there are no errors, then into Python code.

2, query the database

DEF select_db (SQL):
     '' ' query module ' '' 
    # establish a connection to the database 
    DB = pymysql.connect (= Host ' 192.168.0.105 ' , = User ' the root ' , password = ' 123456 ' , DB = ' SZZ ' , Port = 3306, charset = ' UTF8 ' ) 
    CUR = db.cursor ()   # Create object via cursor cursor () 
    cur.execute (SQL) # use execute () Executes SQL 
    Data = cur.fetchall () # Get inside the database All results, returns a two-dimensional array 
    cur.use Close () # close the cursor
    db.Close () # close the connection 
    return Data 
select_sql = select_db ( " SELECT * from STU; " )
 Print (select_sql) 

run results: 
(( . 1, ' test00 ' ), (2, ' Test ' ), (. 3, ' test1 ' ))   # returns to form a nested tuple values 

: If you want to return the nested dictionaries as a list is created when a cursor, to add the following parameters: cursor = pymysql.cursors.DictCursor, namely: CUR db.cursor = (Cursor = pymysql.cursors.DictCursor) 

DEF select_db (SQL):
     '' ' query module ' '' 
    # establish a connection to the database
    pymysql.connect = DB (= Host ' 192.168.0.105 ' , = User ' the root ' , password = ' 123456 ' , DB = ' SZZ ' , Port = 3306, charset = ' UTF8 ' )
     # CUR = db.cursor () # () creates a cursor object via the cursor 
    CUR = db.cursor (the cursor = pymysql.cursors.DictCursor) 
    cur.execute (SQL) # use the execute () Executes SQL 
    the Data = cur.fetchall () # get all the results inside the database, It returns a two-dimensional array 
    cur.close () # close the cursor 
    db.Close () # close the database connection 
    return data
select_sql = select_db("select * from stu;")
print(select_sql)

运行结果:
[{'id': 1, 'name': 'test00'}, {'id': 2, 'name': 'test'}, {'id': 3, 'name': 'test1'}]

note:

or fetchall () function is to obtain the value of all the results inside the database;

either fetchone () function is a database query result obtaining first data

fetchmany (n- ) function is the first N lines of data acquired

Stripes In practice, when the number of queries fetchall a lot of time using the most appropriate, if the query results are only one with a more appropriate fetchone

3, database update operations

DEF select_db (SQL):
     '' ' query module ' '' 
    # establish a connection to the database 
    DB = pymysql.connect (= Host ' 192.168.0.105 ' , = User ' the root ' , password = ' 123456 ' , DB = ' SZZ ' , Port = 3306, charset = ' UTF8 ' )
     # CUR = db.cursor () # Create object via cursor cursor () 
    CUR = db.cursor (cursor = pymysql.cursors.DictCursor) 
    cur.execute (SQL) # use execute () implementation of SQL 
    the db.commit () 
    CUR.use Close () # close the cursor
    db.Close () # close the connection 

select_sql = select_db ( " Update STU SET name = 'test001' WHERE name = 'test00'; " ) 

Second way: 
DEF select_db1 (SQL):
     '' ' query module ' '' 
    # established connection to the database 
    DB = pymysql.connect (= Host ' 192.168.0.105 ' , = User ' the root ' , password = ' 123456 ' , DB = ' SZZ ' , Port = 3306, charset = ' UTF8 ' , = the autocommit True)
     #cur = db.cursor () # () cursor object created by the cursor 
    CUR = db.cursor (cursor = pymysql.cursors.DictCursor) 
    cur.execute (sql) # execute sql using the Execute () 
    cur.close () # close the cursor 
    db.Close () # close the connection 
select_sql = select_db1 ( " Update STU SET name = 'test00' WHERE name = 'test001'; " ) 

# Description: when the connection to the database, if the parameter is added autocommit = True, then after the completion of the implementation of SQL statements without performing the commit operation

4, inserted into the database

DEF select_db1 (SQL):
     '' ' query module ' '' 
    # establish a connection to the database 
    DB = pymysql.connect (= Host ' 192.168.0.105 ' , = User ' the root ' , password = ' 123456 ' , DB = ' SZZ ' , Port = 3306, charset = ' utf8 ' , = autocommit True)
     # CUR = db.cursor () # create a cursor object by the cursor () 
    CUR = db.cursor (the cursor = pymysql.cursors.DictCursor) 
    cur.execute (SQL ) # use execute () Executes SQL 
    CUR.use Close () # close the cursor
    db.Close () # close the connection 
select_sql = select_db1 ( " INSERT INTO STU (ID, name) value (. 5, 'Test3'); " )

5, delete the database data

DEF select_db1 (SQL):
     '' ' query module ' '' 
    # establish a connection to the database 
    DB = pymysql.connect (= Host ' 192.168.0.105 ' , = User ' the root ' , password = ' 123456 ' , DB = ' SZZ ' , Port = 3306, charset = ' utf8 ' , = autocommit True)
     # CUR = db.cursor () # create a cursor object by the cursor () 
    CUR = db.cursor (the cursor = pymysql.cursors.DictCursor) 
    cur.execute (SQL ) # use execute () Executes SQL 
    CUR.use Close () # close the cursor
    db.Close () # close the connection 
select_sql = select_db1 ( " Delete from STU WHERE name = 'test00'; " )

Reference links:

https://www.cnblogs.com/wintest/p/12152687.html

http://www.nnzhp.cn/archives/510

Guess you like

Origin www.cnblogs.com/beginner-boy/p/12564128.html