在上一篇文章里面主要介紹了關于python3連接數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫以及創(chuàng)建表的相關內(nèi)容,在接下來我們試著在我們剛才創(chuàng)建的表中插入數(shù)據(jù),并對其做相關探究。
成都創(chuàng)新互聯(lián)服務項目包括潁泉網(wǎng)站建設、潁泉網(wǎng)站制作、潁泉網(wǎng)頁制作以及潁泉網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網(wǎng)行業(yè)的解決方案,潁泉網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到潁泉省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
#/usr/bin/env python #_*_coding:utf-8_*_ #導入pyMySQL模塊 import pymysql #打開數(shù)據(jù)庫鏈接 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方法操作數(shù)據(jù)庫 cursor.execute(sql) #事務提交 #connect.commit() data=cursor.execute("select * from class order by id desc" ) #使用fetchall方法獲取操作結果 data=cursor.fetchmany(5) print(data) 注意:在這里將事務提交的部分注釋掉了,特演示一下不提交事務的情況。
執(zhí)行結果(執(zhí)行第四次時):
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
檢查數(shù)據(jù)庫中的數(shù)據(jù): 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>
經(jīng)過檢查數(shù)據(jù)庫相關表,我們發(fā)現(xiàn)此時數(shù)據(jù)為空,這是為什么呢,回憶一下我們將注釋事務提交行 connect.commit() 這里就涉及到mysql數(shù)據(jù)庫有關事務的相關知識, 我們試試加上事務會是什么結果呢??
執(zhí)行結果(手動干預過的顯示結果):
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
數(shù)據(jù)庫的查詢結果:
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>
由此我們發(fā)現(xiàn)數(shù)據(jù)庫的事務關系在軟件開發(fā)的過程當中是相當重要的一部分,所以在對事務處理的時候需要嚴謹。
提交事務的源代碼:
#/usr/bin/env python #_*_coding:utf-8_*_ #導入pymysql模塊 import pymysql #打開數(shù)據(jù)庫鏈接 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方法操作數(shù)據(jù)庫 cursor.execute(sql) #事務提交 connect.commit() data=cursor.execute("select * from class order by id desc" ) #使用fetchall方法獲取操作結果 data=cursor.fetchall() print(data)
分享題目:python操作mysql數(shù)據(jù)庫(二)
標題URL:http://bm7419.com/article46/jcighg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、全網(wǎng)營銷推廣、網(wǎng)站建設、網(wǎng)站改版、標簽優(yōu)化、網(wǎng)站設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)