最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python pymsql模塊的使用

 更新時(shí)間:2020年09月07日 11:23:50   作者:云崖先生  
這篇文章主要介紹了Python pymsql模塊的使用,幫助大家我們利用 python 語(yǔ)言與 mysql 進(jìn)行鏈接,感興趣的朋友可以了解下

基本使用

首先要下載 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ù)庫(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í)行sql
sql = "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 之類的操作,必須使用 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í)行sql
sql = "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í)行sql
sql = "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í)行sql
sql = "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)為4
print(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í)行sql
sql = "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)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

乌什县| 桑日县| 澄江县| 沐川县| 克山县| 四平市| 民和| 简阳市| 隆昌县| 宣化县| 鄂尔多斯市| 平邑县| 驻马店市| 凤翔县| 辉南县| 顺平县| 施秉县| 炉霍县| 铜鼓县| 莱西市| 横峰县| 拜泉县| 新龙县| 大同县| 凉山| 萨迦县| 卫辉市| 嘉兴市| 阿克陶县| 东兴市| 皮山县| 浮梁县| 当阳市| 乐东| 建德市| 青海省| 马尔康县| 宕昌县| 章丘市| 舞钢市| 荆门市|