Python pymsql模塊的使用
基本使用
首先要下載 pymysql
pip install pymsql
以下是 pymysql
的基本使用
import pymysql # 鏈接,C/S架構,TCP鏈接 conn = pymysql.connect( host="localhost", database="db1", charset="utf8mb4", user="root", cursorclass=pymysql.cursors.DictCursor, # 記錄結果,字典顯示 # password = "your password", ) # 游標 cursor = conn.cursor() # 執(zhí)行sql sql = "show tables" res = cursor.execute(sql) # 提交執(zhí)行,返回sql影響成功的行數(shù) print(res) # 2 代表該數(shù)據庫下有2個表 print(cursor.fetchall()) # [{'Tables_in_db1': 't1'}, {'Tables_in_db1': 't2'}] cursor.close() # 關閉游標 conn.close()
游標概念
可以看到在上面的示例中有一個游標的概念,其實這個也非常簡單,就等同于光標的上下移動,每移動一次代表一條記錄。
在 pymsql
中,對于 select
等操作返回的結果都可以通過游標的移動配合相應方法函數(shù)來進行讀取。
sql注入
如果你的某些 sql
語句要進行字符串拼接,那么一定要使用 pymysql
提供的 execute()
方法進行拼接,不要去用 python 中的 %
或 format()
方法,這可能導致出現(xiàn) sql 注入問題帶來不安全的隱患。
注意:使用 execute()
時,不可傳入表名,數(shù)據庫名。否則會拋出語法錯誤,這是因為在拼接時會自動添加上``號
import pymysql # 鏈接 conn = pymysql.connect( host="localhost", database="db1", charset="utf8mb4", user="root", cursorclass=pymysql.cursors.DictCursor, # 記錄結果,字典顯示 # password = "your password", ) # 游標 cursor=conn.cursor() # 執(zhí)行sql sql = "select * from t1 where id=%s" res = cursor.execute(sql,("1",)) # 提交執(zhí)行,返回sql影響成功的行數(shù) 這里拼接能預防sql注入問題 print(res) # 1 查出一條記錄 print(cursor.fetchall()) # 拿到所有記錄的結果 cursor.close() # 關閉游標 conn.close()
事務提交
在執(zhí)行 UPDATE/INSERT/DELETE
之類的操作,必須使用 conn.commit()
進行事務提交后方可生效。
或者你可以在實例化 conn
對象時為他指定 auto_commit
參數(shù)為 true
即可自動提交事務。
import pymysql # 鏈接 conn = pymysql.connect( host="localhost", database="db1", charset="utf8mb4", user="root", cursorclass=pymysql.cursors.DictCursor, # 記錄結果,字典顯示 autocommit = True, # 自動提交 # password = "your password", ) # 游標 cursor=conn.cursor() # 執(zhí)行sql sql = "insert into t1(name) values(%s)" res = cursor.execute(sql,("新記錄",)) # 提交執(zhí)行,返回sql影響成功的行數(shù) 這里拼接能預防sql注入問題 print(res) # 1 成功插入一條記錄 print(cursor.lastrowid) #在插入語句后查看,查看最后一條記錄的行號 print(cursor.fetchall()) # conn.commit() # 手動提交 cursor.close() # 關閉游標 conn.close()
提交多條
使用 cursor.executemany()
方法可一次性提交多條 sql 操作。
import pymysql # 鏈接 conn = pymysql.connect( host="localhost", database="db1", charset="utf8mb4", user="root", cursorclass=pymysql.cursors.DictCursor, # 記錄結果,字典顯示 autocommit = True, # 自動提交 # password = "your password", ) # 游標 cursor=conn.cursor() # 執(zhí)行sql sql = "insert into t1(name) values(%s)" # 同一條命令,執(zhí)行3次 res = cursor.executemany(sql,[("新記錄1"),("新紀錄2"),("新紀錄3")]) # 提交執(zhí)行,返回sql影響成功的行數(shù) 這里拼接能預防sql注入問題 print(res) # 3 成功插入三條記錄 print(cursor.lastrowid) #在插入語句后查看,查看最后一條記錄的行號 print(cursor.fetchall()) cursor.close() # 關閉游標 conn.close()
游標相關
獲取到一條記錄后,我們可以控制游標移動。
也可以控制查看游標后的多少條記錄
游標每移動一次代表一條記錄
命令解析 | 描述 |
---|---|
cursor.scroll(3,mode='absolute') | 游標以絕對位置向后移動3條記錄 |
cursor.scroll(3,mode='relative') | 游標以當前位置向后移動3條記錄 |
注意:游標移動的條數(shù)即為記錄的條數(shù),如果移動值為負N就代表上N條記錄 |
如果我們想獲取記錄,可使用以下三個方法
命令解析 | 描述 |
---|---|
cursor.fetchone() | 獲取第一條記錄,游標向下移動一行 |
cursor.fetchmany(2) | 獲取接下來的兩條記錄,游標向下移動兩行 |
cursor.fetchall() | 獲取全部記錄,游標移動到末尾,返回的是一個列表 |
import pymysql # 鏈接 conn = pymysql.connect( host="localhost", database="db1", charset="utf8mb4", user="root", cursorclass=pymysql.cursors.DictCursor, # 記錄結果,字典顯示 autocommit = True, # 自動提交 # password = "your password", ) # 游標 cursor=conn.cursor() # 執(zhí)行sql sql = "select * from t1" # t1表中4條記錄 cursor.execute(sql) print(cursor.fetchone()) 游標移動到2的位置 cursor.scroll(2,mode='relative') 向下移動2,當前游標為4 print(cursor.fetchone()) cursor.close() # 關閉游標 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, # 記錄結果,字典顯示 autocommit = True, # 自動提交 # password = "your password", ) # 游標 cursor=conn.cursor() # 執(zhí)行sql sql = "insert into t1(name) values(%s)" # 同一條命令,執(zhí)行3次 res = cursor.executemany(sql,[("新記錄1"),("新紀錄2"),("新紀錄3")]) # 提交執(zhí)行,返回sql影響成功的行數(shù) 這里拼接能預防sql注入問題 print(res) # 3 成功插入三條記錄 print(cursor.lastrowid) #在插入語句后查看,查看最后一條記錄的行號 print(cursor.fetchall()) # conn.commit() # 手動提交 cursor.close() # 關閉游標 conn.close()
以上就是Python pymsql模塊的使用的詳細內容,更多關于Python pymsql的資料請關注腳本之家其它相關文章!
相關文章
python smtplib發(fā)送多個email聯(lián)系人的實現(xiàn)
這篇文章主要介紹了python smtplib發(fā)送多個email聯(lián)系人的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10Python實現(xiàn)多條件篩選Excel數(shù)據并批量繪制直方圖
這篇文章主要為大家介紹了如何Python對Excel數(shù)據進行多條件篩選和去除并批量繪制直方圖,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2023-09-09