pymysql模塊的操作實(shí)例
pymysql 模塊!
pymysql模塊時(shí)一個(gè)第三方模塊!需要下載:
pymysql的基本使用:
import pymysql conn = pymysql.connect( user = 'root', password = '123', host = '127.0.0.1', # ip地址 port = 3306, # 端口 charset = 'utf8', database = 'day36_1' ) cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)# 產(chǎn)生游標(biāo)對(duì)象 # cursor=pymysql.cursors.DictCursor 將查詢出來的結(jié)果制成字典的形式返回 sql = "select * from userinto" res = cursor.execute(sql) #執(zhí)行sql語句 res = cursor.fetchone() # 打印一條數(shù)據(jù) res = cursor.fetchall() # 可以打印里面的所有數(shù)據(jù) res = cursor.fetchmany(2) #制定獲取幾條數(shù)據(jù),如果數(shù)字超了也不報(bào)錯(cuò) print(res) cursor.scroll(2, 'relative') #相對(duì)移動(dòng) , 基于指針?biāo)谖恢? 往后偏移 cursor.scroll(3, 'absolute') #絕對(duì)移動(dòng),基于起始位置往后偏移 print(cursor.fetchall())
我們可以通過python導(dǎo)入模塊來連接數(shù)據(jù)庫,進(jìn)行登陸注冊(cè)功能,在使用時(shí)sql會(huì)遇到注入問題
sql注入問題 利用特殊符號(hào)和注釋語法 巧妙的繞過真正的sql校驗(yàn),是用戶數(shù)據(jù)不安全
關(guān)鍵性的數(shù)據(jù) 不要自己手動(dòng)去拼接 而是交由execute幫你去做拼接
import pymysql conn = pymysql.connect( user = 'root' , password = '123', host = '127.0.0.1', port = 3306, database = 'day36_1', charset = 'utf8' ) cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #獲取用戶名和密碼,然后取數(shù)據(jù)庫中校驗(yàn) username = input('username>>>:').strip() password = input('password>>>:').strip() sql = "select * from userinto where name=%s and password=%s" print(sql) cursor.execute(sql, (username, password)) #交由execute幫你去做拼接,解決注入問題 res = cursor.fetchall() if res: print(res) else: print('username or password error!')
我們也可以利用pycharm來操作數(shù)據(jù)庫文件的增刪改查!
針對(duì)增 刪 改操作 執(zhí)行重要程度偏高
你如果真想操作 必須有一步確認(rèn)操作(commit)
import pymysql conn = pymysql.connect( user = 'root', passwd = '123456', db = 'day36', host = '127.0.0.1', port = 3306, charset = 'utf8', autocommit = True # 自動(dòng)提交確認(rèn) ) cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # #查: 獲取用戶輸入的用戶名和密碼 然后取數(shù)據(jù)庫中校驗(yàn) # username = input('username>>>:').strip() # password = input('password>>>:').strip() # # sql = "select * from userinfo where name='%s' and password= '%s'"%(username,password) # sql = "select * from userinfo where name=%s and password= %s" # print(sql) # 增 sql = "insert into userinfo(name,password,dep_id) values('jason',789,1)" # 改 # sql = "update userinfo set name='egondsb' where id = 6" # 刪除 # sql = "delete from userinfo where id= 1" res = cursor.execute(sql) # conn.commit() # 確認(rèn)當(dāng)前操作 真正的同步到數(shù)據(jù)庫 print(res)
以上就是相關(guān)知識(shí)點(diǎn)內(nèi)容,感謝大家的學(xué)習(xí)和對(duì)腳本之家的支持。
相關(guān)文章
Python?pyecharts數(shù)據(jù)可視化實(shí)例詳解
PyEcharts是一個(gè)用于生成?Echarts圖表的類庫,?Python是一門富有表達(dá)力的語言,很適合用于數(shù)據(jù)處理,下面這篇文章主要給大家介紹了關(guān)于Python?pyecharts數(shù)據(jù)可視化的相關(guān)資料,需要的朋友可以參考下2022-05-05Python面向?qū)ο笾蓡T相關(guān)知識(shí)總結(jié)
通過面向?qū)ο筮M(jìn)行編程時(shí),會(huì)遇到很多種情況,也會(huì)使用不同的成員來實(shí)現(xiàn),接下來我們來逐一介紹成員特性和應(yīng)用場景,需要的朋友可以參考下2021-06-06python并發(fā)編程多進(jìn)程 模擬搶票實(shí)現(xiàn)過程
這篇文章主要介紹了python并發(fā)編程多進(jìn)程 模擬搶票實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Python讀取含url圖片鏈接的txt文檔方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了三種Python讀取含url圖片鏈接的txt文檔方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04簡要講解Python編程中線程的創(chuàng)建與鎖的使用
這篇文章主要介紹了簡要講解Python編程中線程的創(chuàng)建與鎖的使用,Python中雖然有GIL的存在,但依然是能夠創(chuàng)建多個(gè)線程來交替使用的,需要的朋友可以參考下2016-02-02python3實(shí)現(xiàn)Dijkstra算法最短路徑的實(shí)現(xiàn)
這篇文章主要介紹了python3實(shí)現(xiàn)Dijkstra算法最短路徑的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05