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í)行sql
sql = "show tables"
res = cursor.execute(sql) # 提交執(zhí)行,返回sql影響成功的行數(shù)
print(res) # 2 代表該數(shù)據(jù)庫下有2個表
print(cursor.fetchall()) # [{'Tables_in_db1': 't1'}, {'Tables_in_db1': 't2'}]
cursor.close() # 關(guān)閉游標(biāo)
conn.close()
游標(biāo)概念
可以看到在上面的示例中有一個游標(biāo)的概念,其實這個也非常簡單,就等同于光標(biāo)的上下移動,每移動一次代表一條記錄。

在 pymsql 中,對于 select 等操作返回的結(jié)果都可以通過游標(biāo)的移動配合相應(yīng)方法函數(shù)來進(jìn)行讀取。
sql注入
如果你的某些 sql 語句要進(jìn)行字符串拼接,那么一定要使用 pymysql 提供的 execute() 方法進(jìn)行拼接,不要去用 python 中的 % 或 format() 方法,這可能導(dǎo)致出現(xiàn) sql 注入問題帶來不安全的隱患。
注意:使用 execute() 時,不可傳入表名,數(shù)據(jù)庫名。否則會拋出語法錯誤,這是因為在拼接時會自動添加上``號
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í)行sql
sql = "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ù)提交后方可生效。
或者你可以在實例化 conn 對象時為他指定 auto_commit 參數(shù)為 true 即可自動提交事務(wù)。
import pymysql
# 鏈接
conn = pymysql.connect(
host="localhost",
database="db1",
charset="utf8mb4",
user="root",
cursorclass=pymysql.cursors.DictCursor, # 記錄結(jié)果,字典顯示
autocommit = True, # 自動提交
# password = "your password",
)
# 游標(biāo)
cursor=conn.cursor()
# 執(zhí)行sql
sql = "insert into t1(name) values(%s)"
res = cursor.execute(sql,("新記錄",)) # 提交執(zhí)行,返回sql影響成功的行數(shù) 這里拼接能預(yù)防sql注入問題
print(res) # 1 成功插入一條記錄
print(cursor.lastrowid) #在插入語句后查看,查看最后一條記錄的行號
print(cursor.fetchall())
# conn.commit() # 手動提交
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, # 自動提交
# password = "your password",
)
# 游標(biāo)
cursor=conn.cursor()
# 執(zhí)行sql
sql = "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) #在插入語句后查看,查看最后一條記錄的行號
print(cursor.fetchall())
cursor.close() # 關(guān)閉游標(biāo)
conn.close()
游標(biāo)相關(guān)
獲取到一條記錄后,我們可以控制游標(biāo)移動。
也可以控制查看游標(biāo)后的多少條記錄
游標(biāo)每移動一次代表一條記錄
| 命令解析 | 描述 |
|---|---|
| cursor.scroll(3,mode='absolute') | 游標(biāo)以絕對位置向后移動3條記錄 |
| cursor.scroll(3,mode='relative') | 游標(biāo)以當(dāng)前位置向后移動3條記錄 |
| 注意:游標(biāo)移動的條數(shù)即為記錄的條數(shù),如果移動值為負(fù)N就代表上N條記錄 | |
如果我們想獲取記錄,可使用以下三個方法
| 命令解析 | 描述 |
|---|---|
| cursor.fetchone() | 獲取第一條記錄,游標(biāo)向下移動一行 |
| cursor.fetchmany(2) | 獲取接下來的兩條記錄,游標(biāo)向下移動兩行 |
| cursor.fetchall() | 獲取全部記錄,游標(biāo)移動到末尾,返回的是一個列表 |
import pymysql
# 鏈接
conn = pymysql.connect(
host="localhost",
database="db1",
charset="utf8mb4",
user="root",
cursorclass=pymysql.cursors.DictCursor, # 記錄結(jié)果,字典顯示
autocommit = True, # 自動提交
# password = "your password",
)
# 游標(biāo)
cursor=conn.cursor()
# 執(zhí)行sql
sql = "select * from t1" # t1表中4條記錄
cursor.execute(sql)
print(cursor.fetchone()) 游標(biāo)移動到2的位置
cursor.scroll(2,mode='relative') 向下移動2,當(dāng)前游標(biāo)為4
print(cursor.fetchone())
cursor.close() # 關(guān)閉游標(biāo)
conn.close()
"""
{'id': 1, 'name': '記錄1'}
{'id': 4, 'name': '記錄4'}
"""
插入行號
如果執(zhí)行的是 INSERT 操作,可以在插入后查看最后插入的 ID 行號
import pymysql
# 鏈接
conn = pymysql.connect(
host="localhost",
database="db1",
charset="utf8mb4",
user="root",
cursorclass=pymysql.cursors.DictCursor, # 記錄結(jié)果,字典顯示
autocommit = True, # 自動提交
# password = "your password",
)
# 游標(biāo)
cursor=conn.cursor()
# 執(zhí)行sql
sql = "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) #在插入語句后查看,查看最后一條記錄的行號
print(cursor.fetchall())
# conn.commit() # 手動提交
cursor.close() # 關(guān)閉游標(biāo)
conn.close()
以上就是Python pymsql模塊的使用的詳細(xì)內(nèi)容,更多關(guān)于Python pymsql的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python smtplib發(fā)送多個email聯(lián)系人的實現(xiàn)
這篇文章主要介紹了python smtplib發(fā)送多個email聯(lián)系人的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Python實現(xiàn)多條件篩選Excel數(shù)據(jù)并批量繪制直方圖
這篇文章主要為大家介紹了如何Python對Excel數(shù)據(jù)進(jìn)行多條件篩選和去除并批量繪制直方圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-09-09
使用 Python 玩轉(zhuǎn) GitHub 的貢獻(xiàn)板(推薦)
這篇文章主要介紹了使用 Python 玩轉(zhuǎn) GitHub 的貢獻(xiàn)板的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
使用Python的Flask框架來搭建第一個Web應(yīng)用程序
Flask框架是一個以輕量級著稱的Web開發(fā)框架,近兩年來在Web領(lǐng)域獲得了極高的人氣,這里我們就來看如何使用Python的Flask框架來搭建第一個Web應(yīng)用程序2016-06-06

