Python中pymysql 模塊的使用詳解
pymysql 模塊的使用
一、pymysql的下載和使用
(1)pymysql模塊的下載
pip3 install pymysql
(2)pymysql的使用
# 實現(xiàn):使用Python實現(xiàn)用戶登錄,如果用戶存在則登錄成功(假設(shè)該用戶已在數(shù)據(jù)庫中)
import pymysql
user = input('請輸入用戶名:')
pwd = input('請輸入密碼:')
# 1.連接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', db='db8', charset='utf8')
# 2.創(chuàng)建游標(biāo)
cursor = conn.cursor()
#注意%s需要加引號
sql = "select * from userinfo where username='%s' and pwd='%s'" %(user, pwd)
print(sql)
# 3.執(zhí)行sql語句
cursor.execute(sql)
result=cursor.execute(sql) #執(zhí)行sql語句,返回sql查詢成功的記錄數(shù)目
print(result)
# 關(guān)閉連接,游標(biāo)和連接都要關(guān)閉
cursor.close()
conn.close()
if result:
print('登陸成功')
else:
print('登錄失敗')
二、execute()之sql注入
最后那一個空格,在一條sql語句中如果遇到select * from userinfo where username='mjj' -- asadasdas' and pwd='' 則--之后的條件被注釋掉了(注意--后面還有一個空格)
#1、sql注入之:用戶存在,繞過密碼
mjj' -- 任意字符
#2、sql注入之:用戶不存在,繞過用戶與密碼
xxx' or 1=1 -- 任意字符
解決方法:
# 原來是我們對sql進行字符串拼接 # sql="select * from userinfo where name='%s' and password='%s'" %(username,pwd) # print(sql) # result=cursor.execute(sql) #改寫為(execute幫我們做字符串拼接,我們無需且一定不能再為%s加引號了) sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引號,因為pymysql會自動為我們加上 result=cursor.execute(sql,[user,pwd]) #pymysql模塊自動幫我們解決sql注入的問題,只要我們按照pymysql的規(guī)矩來。
三、增、刪、改:conn.commit()
commit()方法:在數(shù)據(jù)庫里增、刪、改的時候,必須要進行提交,否則插入的數(shù)據(jù)不生效。
import pymysql
username = input('請輸入用戶名:')
pwd = input('請輸入密碼:')
# 1.連接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8')
# 2.創(chuàng)建游標(biāo)
cursor = conn.cursor()
# 操作
# 增
# sql = "insert into userinfo(username,pwd) values (%s,%s)"
# effect_row = cursor.execute(sql,(username,pwd))
#同時插入多條數(shù)據(jù)
#cursor.executemany(sql,[('李四','110'),('王五','119')])
# print(effect_row)#
# 改
# sql = "update userinfo set username = %s where id = 2"
# effect_row = cursor.execute(sql,username)
# print(effect_row)
# 刪
sql = "delete from userinfo where id = 2"
effect_row = cursor.execute(sql)
print(effect_row)
#一定記得commit
conn.commit()
# 4.關(guān)閉游標(biāo)
cursor.close()
# 5.關(guān)閉連接
conn.close()
四、查:fetchone、fetchmany、fetchall
fetchone():獲取下一行數(shù)據(jù),第一次為首行;
fetchall():獲取所有行數(shù)據(jù)源
fetchmany(4):獲取4行數(shù)據(jù)
查看一下表內(nèi)容:
mysql> select * from userinfo; +----+----------+-----+ | id | username | pwd | +----+----------+-----+ | 1 | mjj | 123 | | 3 | 張三 | 110 | | 4 | 李四 | 119 | +----+----------+-----+ 3 rows in set (0.00 sec)
使用fetchone():
import pymysql
# 1.連接 conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8') # 2.創(chuàng)建游標(biāo) cursor = conn.cursor() sql = 'select * from userinfo' cursor.execute(sql) # 查詢第一行的數(shù)據(jù) row = cursor.fetchone() print(row) # (1, 'mjj', '123') # 查詢第二行數(shù)據(jù) row = cursor.fetchone() print(row) # (3, '張三', '110') # 4.關(guān)閉游標(biāo) cursor.close() # 5.關(guān)閉連接 conn.close()
使用fetchall():
import pymysql # 1.連接 conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8') # 2.創(chuàng)建游標(biāo) cursor = conn.cursor() sql = 'select * from userinfo' cursor.execute(sql) # 獲取所有的數(shù)據(jù) rows = cursor.fetchall() print(rows) # 4.關(guān)閉游標(biāo) cursor.close() # 5.關(guān)閉連接 conn.close() #運行結(jié)果 ((1, 'mjj', '123'), (3, '張三', '110'), (4, '李四', '119'))
默認情況下,我們獲取到的返回值是元組,只能看到每行的數(shù)據(jù),卻不知道每一列代表的是什么,這個時候可以使用以下方式來返回字典,每一行的數(shù)據(jù)都會生成一個字典:
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #在實例化的時候,將屬性cursor設(shè)置為pymysql.cursors.DictCursor
在fetchone示例中,在獲取行數(shù)據(jù)的時候,可以理解開始的時候,有一個行指針指著第一行的上方,獲取一行,它就向下移動一行,所以當(dāng)行指針到最后一行的時候,就不能再獲取到行的內(nèi)容,所以我們可以使用如下方法來移動行指針:
cursor.scroll(1,mode='relative') # 相對當(dāng)前位置移動
cursor.scroll(2,mode='absolute') # 相對絕對位置移動
第一個值為移動的行數(shù),整數(shù)為向下移動,負數(shù)為向上移動,mode指定了是相對當(dāng)前位置移動,還是相對于首行移動
# 1.Python實現(xiàn)用戶登錄
# 2.Mysql保存數(shù)據(jù)
import pymysql
# 1.連接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8')
# 2.創(chuàng)建游標(biāo)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = 'select * from userinfo'
cursor.execute(sql)
# 查詢第一行的數(shù)據(jù)
row = cursor.fetchone()
print(row) # (1, 'mjj', '123')
# 查詢第二行數(shù)據(jù)
row = cursor.fetchone() # (3, '張三', '110')
print(row)
cursor.scroll(-1,mode='relative') #設(shè)置之后,光標(biāo)相對于當(dāng)前位置往前移動了一行,所以打印的結(jié)果為第二行的數(shù)據(jù)
row = cursor.fetchone()
print(row)
cursor.scroll(0,mode='absolute') #設(shè)置之后,光標(biāo)相對于首行沒有任何變化,所以打印的結(jié)果為第一行數(shù)據(jù)
row = cursor.fetchone()
print(row)
# 4.關(guān)閉游標(biāo)
cursor.close()
# 5.關(guān)閉連接
conn.close()
#結(jié)果如下
{'id': 1, 'username': 'mjj', 'pwd': '123'}
{'id': 3, 'username': '張三', 'pwd': '110'}
{'id': 3, 'username': '張三', 'pwd': '110'}
{'id': 1, 'username': 'mjj', 'pwd': '123'}
fetchmany():
import pymysql
# 1.連接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8')
# 2.創(chuàng)建游標(biāo)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = 'select * from userinfo'
cursor.execute(sql)
# 獲取2條數(shù)據(jù)
rows = cursor.fetchmany(2)
print(rows)
# 4.關(guān)閉游標(biāo)
# rows = cursor.fetchall()
# print(rows)
cursor.close()
# 5.關(guān)閉連接
conn.close()
#結(jié)果如下:
[{'id': 1, 'username': 'mjj', 'pwd': '123'}, {'id': 3, 'username': '張三', 'pwd': '110'}]
- Python中操作mysql的pymysql模塊詳解
- Python中模塊pymysql查詢結(jié)果后如何獲取字段列表
- 使用python連接mysql數(shù)據(jù)庫之pymysql模塊的使用
- Python 中使用 PyMySQL模塊操作數(shù)據(jù)庫的方法
- python之pymysql模塊簡單應(yīng)用示例代碼
- Python使用pymysql模塊操作mysql增刪改查實例分析
- Python 解析pymysql模塊操作數(shù)據(jù)庫的方法
- Python pymysql模塊安裝并操作過程解析
- python使用pymysql模塊操作MySQL
- Python中使用PyMySQL模塊的方法詳解
相關(guān)文章
Selenium獲取登錄Cookies并添加Cookies自動登錄的方法
這篇文章主要介紹了Selenium獲取登錄Cookies并添加Cookies自動登錄的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python中canmatrix解析dbc文件應(yīng)用常見示例
DBC是一種用于描述CAN網(wǎng)絡(luò)協(xié)議的文件格式,Python作為一種流行的編程語言,也提供了解析DBC文件的相關(guān)類庫,這篇文章主要給大家介紹了關(guān)于Python中canmatrix解析dbc文件的相關(guān)資料,需要的朋友可以參考下2024-08-08
基于 Django 的手機管理系統(tǒng)實現(xiàn)過程詳解
這篇文章主要介紹了基于 Django 的手機管理系統(tǒng)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
Python中xml和dict格式轉(zhuǎn)換的示例代碼
最近在做APP的接口,遇到XML格式的請求數(shù)據(jù),費了很大勁來解決,下面小編給大家分享下Python中xml和dict格式轉(zhuǎn)換問題,感興趣的朋友跟隨小編一起看看吧2019-11-11

