pymysql模塊的使用(增刪改查)詳解
一、pymysql的下載和使用
之前我們都是通過MySQL自帶的命令行客戶端工具mysql來操作數(shù)據(jù)庫,那如何在python程序中操作數(shù)據(jù)庫呢?這就用到了pymysql模塊,該模塊本質(zhì)就是一個套接字客戶端軟件,使用前需要事先安裝。
(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)建游標 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)閉連接,游標和連接都要關(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)建游標 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)閉游標 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 | +----+----------+-----+ 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)建游標 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)閉游標 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)建游標 cursor = conn.cursor() sql = 'select * from userinfo' cursor.execute(sql) # 獲取所有的數(shù)據(jù) rows = cursor.fetchall() print(rows) # 4.關(guān)閉游標 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)建游標 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è)置之后,光標相對于當(dāng)前位置往前移動了一行,所以打印的結(jié)果為第二行的數(shù)據(jù) row = cursor.fetchone() print(row) cursor.scroll(0,mode='absolute') #設(shè)置之后,光標相對于首行沒有任何變化,所以打印的結(jié)果為第一行數(shù)據(jù) row = cursor.fetchone() print(row) # 4.關(guān)閉游標 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)建游標 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)閉游標 # rows = cursor.fetchall() # print(rows) cursor.close() # 5.關(guān)閉連接 conn.close() #結(jié)果如下: [{'id': 1, 'username': 'mjj', 'pwd': '123'}, {'id': 3, 'username': '張三', 'pwd': '110'}]
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Django項目中模板標簽及模板的繼承與引用(網(wǎng)站中快速布置廣告)
這篇文章主要介紹了詳解Django項目中模板標簽及模板的繼承與引用【網(wǎng)站中快速布置廣告】,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03Python通過樸素貝葉斯和LSTM分別實現(xiàn)新聞文本分類
樸素貝葉斯法(Naive Bayes model)是基于貝葉斯定理與特征條件獨立假設(shè)的分類方法。LSTM則是一種時間循環(huán)神經(jīng)網(wǎng)絡(luò),適合于處理和預(yù)測時間序列中間隔和延遲相對較長的重要事件。本文將通過這兩個方法分別實現(xiàn)新聞文本分類,需要的可以參考一下2021-12-12在arcgis使用python腳本進行字段計算時是如何解決中文問題的
這篇文章主要介紹了在arcgis使用python腳本進行字段計算時是如何解決中文問題的,需要的朋友可以參考下2015-10-10PyQt中實現(xiàn)自定義工具提示ToolTip的方法詳解
這篇文章主要為大家詳細介紹了PyQt中實現(xiàn)自定義工具提示ToolTip的方法詳解,文中的示例代碼講解詳細,對我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-05-05Python之time模塊的時間戳,時間字符串格式化與轉(zhuǎn)換方法(13位時間戳)
今天小編就為大家分享一篇Python之time模塊的時間戳,時間字符串格式化與轉(zhuǎn)換方法(13位時間戳),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08利用Python+阿里云實現(xiàn)DDNS動態(tài)域名解析的方法
這篇文章主要介紹了利用Python+阿里云實現(xiàn)DDNS動態(tài)域名解析的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04