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進(jìn)行字符串拼接 # 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ù)庫里增、刪、改的時候,必須要進(jìn)行提交,否則插入的數(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'))
默認(rèn)情況下,我們獲取到的返回值是元組,只能看到每行的數(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ù)為向下移動,負(fù)數(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)文章
python數(shù)據(jù)抓取分析的示例代碼(python + mongodb)
本篇文章主要介紹了python數(shù)據(jù)抓取分析的示例代碼(python + mongodb),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12django數(shù)據(jù)關(guān)系一對多、多對多模型、自關(guān)聯(lián)的建立
這篇文章主要介紹了django數(shù)據(jù)關(guān)系一對多、多對多模型、自關(guān)聯(lián)的建立2019-07-07Python semaphore evevt生產(chǎn)者消費者模型原理解析
這篇文章主要介紹了Python semaphore evevt生產(chǎn)者消費者模型原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03python通過ssh-powershell監(jiān)控windows的方法
這篇文章主要介紹了python通過ssh-powershell監(jiān)控windows的方法,涉及Python操作ssh-powershell的相關(guān)技巧,需要的朋友可以參考下2015-06-06Python實現(xiàn)操作Redis所有類型的方法詳解
Redis作為一款高性能的NoSQL數(shù)據(jù)庫,越來越受到了廣大開發(fā)者的喜愛。本篇博客將介紹如何使用Python操作Redis的所有類型,以及一些高級用法,感興趣的可以了解一下2023-04-04