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é)果都可以通過游標(biāo)的移動(dòng)配合相應(yīng)方法函數(shù)來進(jìn)行讀取。
sql注入
如果你的某些 sql 語(yǔ)句要進(jìn)行字符串拼接,那么一定要使用 pymysql 提供的 execute() 方法進(jìn)行拼接,不要去用 python 中的 % 或 format() 方法,這可能導(dǎo)致出現(xiàn) sql 注入問題帶來不安全的隱患。
注意:使用 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注入問題print(res) # 1 查出一條記錄print(cursor.fetchall()) # 拿到所有記錄的結(jié)果cursor.close() # 關(guān)閉游標(biāo)conn.close()
事務(wù)提交
在執(zhí)行 UPDATE/INSERT/DELETE 之類的操作,必須使用 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注入問題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注入問題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) 獲取接下來的兩條記錄,游標(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注入問題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. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令2. Xml簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. 使用Hangfire+.NET 6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)4. 如何在jsp界面中插入圖片5. phpstudy apache開啟ssi使用詳解6. jsp實(shí)現(xiàn)登錄驗(yàn)證的過濾器7. jsp文件下載功能實(shí)現(xiàn)代碼8. 詳解瀏覽器的緩存機(jī)制9. 爬取今日頭條Ajax請(qǐng)求10. xml中的空格之完全解說
