在上一篇文章裏面主要介紹了關於python3鏈接數據庫,建立數據庫以及建立表的相關內容,在接下來咱們試着在咱們剛纔建立的表中插入數據,並對其作相關探究。python
#/usr/bin/env python #_*_coding:utf-8_*_ #導入pymysql模塊 import pymysql #打開數據庫連接 connect=pymysql.connect(host="192.168.186.157",port=3306,user="winner",passwd="123123",db="pymysql",charset="utf8",connect_timeout=3000) #使用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) 注意:在這裏將事務提交的部分註釋掉了,特演示一下不提交事務的狀況。
執行結果(執行第四次時):mysql
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
檢查數據庫中的數據: 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>
通過檢查數據庫相關表,咱們發現此時數據爲空,這是爲何呢,回憶一下咱們將註釋事務提交行 connect.commit() 這裏就涉及到mysql數據庫有關事務的相關知識, 咱們試試加上事務會是什麼結果呢??
執行結果(手動干預過的顯示結果):sql
C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe C:/Users/Administrator/PycharmProjects/python/insertmysql.py ((28, 'cursor', 'execute'), (27, 'GOOD', 'contine'), (26, 'winner', 'back'), (25, 'JSP', 'go'), (24, 'cursor', 'execute'), (23, 'GOOD', 'contine'), (22, 'winner', 'back'), (21, 'JSP', 'go'), (20, 'cursor', 'execute'), (19, 'GOOD', 'contine'), (18, 'winner', 'back'), (17, 'JSP', 'go'), (16, 'cursor', 'execute'), (15, 'GOOD', 'contine'), (14, 'winner', 'back'), (13, 'JSP', 'go')) Process finished with exit code 0
數據庫的查詢結果:數據庫
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>
由此咱們發現數據庫的事務關係在軟件開發的過程中是至關重要的一部分,因此在對事務處理的時候須要嚴謹。ide
提交事務的源代碼:fetch
#/usr/bin/env python #_*_coding:utf-8_*_ #導入pymysql模塊 import pymysql #打開數據庫連接 connect=pymysql.connect(host="192.168.186.157",port=3306,user="winner",passwd="123123",db="pymysql",charset="utf8",connect_timeout=3000) #使用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.fetchall() print(data)