In the previous article, we focused on the Python3 connection database, creating a database and creating tables, and then we tried to insert the data into the table we just created and explore it.
#/usr/bin/env python#_*_coding:utf-8_*_# Importing Pymysql module import pymysql# Open Database link connect=pymysql.connect (host= " 192.168.186.157 ", port=3306,user=" winner ", passwd=" 123123 ", db=" Pymysql ", charset=" UTF8 ", connect_timeout=3000) # Use the cursor method to get the operation Cursor cursor=connect.cursor () sql= "INSERT into Class (name,address) VALUES (" JSP "," Go "), (" Winner "," Back " ), ("Good", "Contine"), ("Cursor", "execute"); #使用execute方法操作数据库cursor. Execute (SQL) #事务提交 #connect.commit () data=cursor.execute ("SELECT * from class ORDER by id DESC") #使用fetchall方法获取操作结果data =cursor.fetchmany (5) print (data) Note: The section that commits the transaction is commented out here, which demonstrates the case of not committing the transaction.
Execution results (at the time of the fourth execution):
c:\users\administrator\appdata\local\programs\python\python35\ python.exe c:/users/administrator/pycharmprojects/python/insertmysql.py ((12, ' cursor ', ' Execute '), (11, ' good ', ' contine '), (10, ' winner ', ' Back '), (9, ' JSP ', ') Go ')) process finished with exit code 0
Check the data in the database:mysql> Select Database (); +------------+| Database () |+------------+| Pymysql |+------------+1 row in Set (0.00 sec) mysql> Show tables;+-------------------+| Tables_in_pymysql |+-------------------+| Class |+-------------------+1 row in Set (0.00 sec) mysql> SELECT * from class; Empty Set (0.00 sec) mysql>
After checking the database related tables, we found that the data is empty, this is why, recall that we will comment on the transaction submission line Connect.commit () Here is related to the MySQL database relevant knowledge of the transaction, we try to add the result of the transaction?
Execution results (manually intervened display results):
C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe c:/users/administrator/pycharmprojects /python/insertmysql.py ((), ' cursor ', ' execute '), (+, ' good ', ' contine '), (+, ' winner ', ' Back '), (+, ' JSP ', ' Go '), (24, ' Cursor ', ' execute '), (max, ' good ', ' contine '), (+, ' winner ', ' Back '), (+, ' JSP ', ' Go '), (+, ' cursor ', ' execute '), (1 9, ' good ', ' contine '), (+, ' winner ', ' Back '), (+, ' JSP ', ' Go '), (+, ' cursor ', ' execute '), (+, ' good ', ' contine '), (1 4, ' winner ', ' Back '), (+, ' JSP ', ' Go ')) Process finished with exit code 0
Query results for the database:
mysql> select * from class;+----+--------+---------+| id | name | address |+----+--------+---------+| 13 | jsp | go | | 14 | winner | back | | 15 | good | contine | | 16 | cursor | execute | | 17 | jsp | go | | 18 | winner | back | | 19 | good | contine | | 20 | cursor | execute | | 21 | jsp | go | | 22 | winner | back | | 23 | good | contine | | 24 | cursor | execute | | 25 | jsp | go | | 26 | winner | back | | 27 | good | contine | | 28 | cursor | execute |+----+--------+---------+16 rows in set (0.00 SEC) mysql>
Therefore, we find that the transaction relationship of database is a very important part in the process of software development, so we need to be rigorous in dealing with the transaction.
Source code to commit the transaction:
#/usr/bin/env python#_*_coding:utf-8_*_# Importing Pymysql module import pymysql# Open Database link connect=pymysql.connect (host= " 192.168.186.157 ", port=3306,user=" winner ", passwd=" 123123 ", db=" Pymysql ", charset=" UTF8 ", connect_timeout=3000) # Use the cursor method to get the operation Cursor cursor=connect.cursor () sql= "INSERT into Class (name,address) VALUES (" JSP "," Go "), (" Winner "," Back " ), ("Good", "Contine"), ("Cursor", "execute"); #使用execute方法操作数据库cursor. Execute (SQL) #事务提交connect. Commit () Data=cursor.execute ("SELECT * from class ORDER by id DESC") # Get operation results using the Fetchall method Data=cursor.fetchall () print (data)
This article is from the "Keep Dreaming" blog, please be sure to keep this source http://dreamlinux.blog.51cto.com/9079323/1907607
Python operation MySQL Database (ii)