Python pymsql模塊的使用
基本使用
首先要下載 pymysql
pip install pymsql
以下是 pymysql 的基本使用
import pymysql# 鏈接,C/S架構(gòu),TCP鏈接conn = pymysql.connect( host='localhost', database='db1', charset='utf8mb4', user='root', cursorclass=pymysql.cursors.DictCursor, # 記錄結(jié)果,字典顯示 # password = 'your password', ) # 游標(biāo)cursor = conn.cursor()# 執(zhí)行sqlsql = 'show tables'res = cursor.execute(sql) # 提交執(zhí)行,返回sql影響成功的行數(shù)print(res) # 2 代表該數(shù)據(jù)庫(kù)下有2個(gè)表print(cursor.fetchall()) # [{’Tables_in_db1’: ’t1’}, {’Tables_in_db1’: ’t2’}]cursor.close() # 關(guān)閉游標(biāo)conn.close()
游標(biāo)概念
可以看到在上面的示例中有一個(gè)游標(biāo)的概念,其實(shí)這個(gè)也非常簡(jiǎn)單,就等同于光標(biāo)的上下移動(dòng),每移動(dòng)一次代表一條記錄。
在 pymsql 中,對(duì)于 select 等操作返回的結(jié)果都可以通過(guò)游標(biāo)的移動(dòng)配合相應(yīng)方法函數(shù)來(lái)進(jìn)行讀取。
sql注入
如果你的某些 sql 語(yǔ)句要進(jìn)行字符串拼接,那么一定要使用 pymysql 提供的 execute() 方法進(jìn)行拼接,不要去用 python 中的 % 或 format() 方法,這可能導(dǎo)致出現(xiàn) sql 注入問(wèn)題帶來(lái)不安全的隱患。
注意:使用 execute() 時(shí),不可傳入表名,數(shù)據(jù)庫(kù)名。否則會(huì)拋出語(yǔ)法錯(cuò)誤,這是因?yàn)樵谄唇訒r(shí)會(huì)自動(dòng)添加上``號(hào)
import pymysql# 鏈接conn = pymysql.connect( host='localhost', database='db1', charset='utf8mb4', user='root', cursorclass=pymysql.cursors.DictCursor, # 記錄結(jié)果,字典顯示 # password = 'your password', ) # 游標(biāo)cursor=conn.cursor()# 執(zhí)行sqlsql = 'select * from t1 where id=%s'res = cursor.execute(sql,('1',)) # 提交執(zhí)行,返回sql影響成功的行數(shù) 這里拼接能預(yù)防sql注入問(wèn)題print(res) # 1 查出一條記錄print(cursor.fetchall()) # 拿到所有記錄的結(jié)果cursor.close() # 關(guān)閉游標(biāo)conn.close()
事務(wù)提交
在執(zhí)行 UPDATE/INSERT/DELETE 之類(lèi)的操作,必須使用 conn.commit() 進(jìn)行事務(wù)提交后方可生效。
或者你可以在實(shí)例化 conn 對(duì)象時(shí)為他指定 auto_commit 參數(shù)為 true 即可自動(dòng)提交事務(wù)。
import pymysql# 鏈接conn = pymysql.connect( host='localhost', database='db1', charset='utf8mb4', user='root', cursorclass=pymysql.cursors.DictCursor, # 記錄結(jié)果,字典顯示 autocommit = True, # 自動(dòng)提交 # password = 'your password', ) # 游標(biāo)cursor=conn.cursor()# 執(zhí)行sqlsql = 'insert into t1(name) values(%s)'res = cursor.execute(sql,('新記錄',)) # 提交執(zhí)行,返回sql影響成功的行數(shù) 這里拼接能預(yù)防sql注入問(wèn)題print(res) # 1 成功插入一條記錄print(cursor.lastrowid) #在插入語(yǔ)句后查看,查看最后一條記錄的行號(hào)print(cursor.fetchall())# conn.commit() # 手動(dòng)提交cursor.close() # 關(guān)閉游標(biāo)conn.close()
提交多條
使用 cursor.executemany() 方法可一次性提交多條 sql 操作。
import pymysql# 鏈接conn = pymysql.connect( host='localhost', database='db1', charset='utf8mb4', user='root', cursorclass=pymysql.cursors.DictCursor, # 記錄結(jié)果,字典顯示 autocommit = True, # 自動(dòng)提交 # password = 'your password', ) # 游標(biāo)cursor=conn.cursor()# 執(zhí)行sqlsql = 'insert into t1(name) values(%s)' # 同一條命令,執(zhí)行3次res = cursor.executemany(sql,[('新記錄1'),('新紀(jì)錄2'),('新紀(jì)錄3')]) # 提交執(zhí)行,返回sql影響成功的行數(shù) 這里拼接能預(yù)防sql注入問(wèn)題print(res) # 3 成功插入三條記錄print(cursor.lastrowid) #在插入語(yǔ)句后查看,查看最后一條記錄的行號(hào)print(cursor.fetchall())cursor.close() # 關(guān)閉游標(biāo)conn.close()
游標(biāo)相關(guān)
獲取到一條記錄后,我們可以控制游標(biāo)移動(dòng)。
也可以控制查看游標(biāo)后的多少條記錄
游標(biāo)每移動(dòng)一次代表一條記錄
命令解析 描述 cursor.scroll(3,mode=’absolute’) 游標(biāo)以絕對(duì)位置向后移動(dòng)3條記錄 cursor.scroll(3,mode=’relative’) 游標(biāo)以當(dāng)前位置向后移動(dòng)3條記錄 注意:游標(biāo)移動(dòng)的條數(shù)即為記錄的條數(shù),如果移動(dòng)值為負(fù)N就代表上N條記錄
如果我們想獲取記錄,可使用以下三個(gè)方法
命令解析 描述 cursor.fetchone() 獲取第一條記錄,游標(biāo)向下移動(dòng)一行 cursor.fetchmany(2) 獲取接下來(lái)的兩條記錄,游標(biāo)向下移動(dòng)兩行 cursor.fetchall() 獲取全部記錄,游標(biāo)移動(dòng)到末尾,返回的是一個(gè)列表
import pymysql# 鏈接conn = pymysql.connect( host='localhost', database='db1', charset='utf8mb4', user='root', cursorclass=pymysql.cursors.DictCursor, # 記錄結(jié)果,字典顯示 autocommit = True, # 自動(dòng)提交 # password = 'your password', ) # 游標(biāo)cursor=conn.cursor()# 執(zhí)行sqlsql = 'select * from t1' # t1表中4條記錄cursor.execute(sql)print(cursor.fetchone()) 游標(biāo)移動(dòng)到2的位置cursor.scroll(2,mode=’relative’) 向下移動(dòng)2,當(dāng)前游標(biāo)為4print(cursor.fetchone())cursor.close() # 關(guān)閉游標(biāo)conn.close()'''{’id’: 1, ’name’: ’記錄1’}{’id’: 4, ’name’: ’記錄4’}'''
插入行號(hào)
如果執(zhí)行的是 INSERT 操作,可以在插入后查看最后插入的 ID 行號(hào)
import pymysql# 鏈接conn = pymysql.connect( host='localhost', database='db1', charset='utf8mb4', user='root', cursorclass=pymysql.cursors.DictCursor, # 記錄結(jié)果,字典顯示 autocommit = True, # 自動(dòng)提交 # password = 'your password', ) # 游標(biāo)cursor=conn.cursor()# 執(zhí)行sqlsql = 'insert into t1(name) values(%s)' # 同一條命令,執(zhí)行3次res = cursor.executemany(sql,[('新記錄1'),('新紀(jì)錄2'),('新紀(jì)錄3')]) # 提交執(zhí)行,返回sql影響成功的行數(shù) 這里拼接能預(yù)防sql注入問(wèn)題print(res) # 3 成功插入三條記錄print(cursor.lastrowid) #在插入語(yǔ)句后查看,查看最后一條記錄的行號(hào)print(cursor.fetchall())# conn.commit() # 手動(dòng)提交cursor.close() # 關(guān)閉游標(biāo)conn.close()
以上就是Python pymsql模塊的使用的詳細(xì)內(nèi)容,更多關(guān)于Python pymsql的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 如何在jsp界面中插入圖片2. 如何通過(guò)vscode運(yùn)行調(diào)試javascript代碼3. HTML <!DOCTYPE> 標(biāo)簽4. JS數(shù)據(jù)類(lèi)型判斷的幾種常用方法5. Ajax實(shí)現(xiàn)頁(yè)面無(wú)刷新留言效果6. WML語(yǔ)言的基本情況7. python基于tkinter制作無(wú)損音樂(lè)下載工具(附源碼)8. ajax請(qǐng)求后臺(tái)得到j(luò)son數(shù)據(jù)后動(dòng)態(tài)生成樹(shù)形下拉框的方法9. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能10. 用Python實(shí)現(xiàn)定時(shí)備份Mongodb數(shù)據(jù)并上傳到FTP服務(wù)器
