【目錄】
一、IDE工具介紹與安裝使用——navicat 神器
二、MySQL資料備份
三、pymysql模塊
1、安裝
2、使用模塊
一、IDE工具介紹與安裝使用——navicat 神器
https://www.cnblogs.com/bigorangecc/p/12851845.html
二、MySQL資料備份
https://zhuanlan.zhihu.com/p/115504709
三、pymysql模塊
模塊安裝包下載官網:https://pypi.org/search/?q=pymysql
其他下載地址,參考博客 https://www.cnblogs.com/bigorangecc/p/12852337.html
1、安裝
方法一:cmd 中輸入指令 pip install pymysql (pip 未規定具體版本,具體情況而定)

方法二:直接在 pycharm中安裝
(1)傻瓜式
先新建一個python檔案,輸入代碼
import pymysql
此時你會發現,pymysql下面標有紅線,游標移至pymysql,查看錯誤提示,神奇地發現 有“install package pymysql”,點擊即可安裝
(2)專業式
pycharm 軟體中,選單欄【檔案】——【設定】——【專案】——【Project Interpreter】——點擊 + 號

搜索需要安裝的模塊包

【安裝時可能會遇到的問題--時區問題】
參考:https://www.cnblogs.com/arxive/p/10332254.html
方法三:使用Anaconda進行安裝
(待續)
2、使用模塊
(1)鏈接、執行sql、關閉(游標)
import pymysql user=input('用戶名: ').strip() pwd=input('密碼: ').strip() #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon',charset='utf8') #游標 cursor=conn.cursor() #執行完畢回傳的結果集默認以元組顯示 #cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) #執行sql陳述句 sql='select * from userinfo where name="%s" and password="%s"' %(user,pwd) #注意%s需要加引號 print(sql) res=cursor.execute(sql) #執行sql陳述句,回傳sql查詢成功的記錄數目 print(res) cursor.close() conn.close() if res: print('登錄成功') else: print('登錄失敗')
(2)execute()之sql注入
注意:符號--會注釋掉它之后的sql,正確的語法:--后至少有一個任意字符
根本原理:就根據程式的字串拼接name='%s',我們輸入一個xxx' -- haha,用我們輸入的xxx加'在程式中拼接成一個判斷條件name='xxx' -- haha'
最后那一個空格,在一條sql陳述句中如果遇到select * from t1 where id > 3 -- and name='egon';則--之后的條件被注釋掉了 #1、sql注入之:用戶存在,繞過密碼 egon' -- 任意字符 #2、sql注入之:用戶不存在,繞過用戶與密碼 xxx' or 1=1 -- 任意字符
解決方法:
# 原來是我們自己對sql進行字串拼接 # sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd) # print(sql) # res=cursor.execute(sql) #改寫為(execute幫我們做字串拼接,我們無需且一定不能再為%s加引號了) sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引號,因為pymysql會自動為我們加上 res=cursor.execute(sql,[user,pwd]) #pymysql模塊自動幫我們解決sql注入的問題,只要我們按照pymysql的規矩來,
(3)增、刪、改:conn.commit()
import pymysql #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') #游標 cursor=conn.cursor() #執行sql陳述句 #part1 # sql='insert into userinfo(name,password) values("root","123456");' # res=cursor.execute(sql) #執行sql陳述句,回傳sql影響成功的行數 # print(res) #part2 # sql='insert into userinfo(name,password) values(%s,%s);' # res=cursor.execute(sql,("root","123456")) #執行sql陳述句,回傳sql影響成功的行數 # print(res) #part3 sql='insert into userinfo(name,password) values(%s,%s);' res=cursor.executemany(sql,[("root","123456"),("lhf","12356"),("eee","156")]) #執行sql陳述句,回傳sql影響成功的行數 print(res) conn.commit() #提交后才發現表中插入記錄成功 cursor.close() conn.close()
(4)查:fetchone,fetchmany,fetchall
import pymysql #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') #游標 cursor=conn.cursor() #執行sql陳述句 sql='select * from userinfo;' rows=cursor.execute(sql) #執行sql陳述句,回傳sql影響成功的行數rows,將結果放入一個集合,等待被查詢 # cursor.scroll(3,mode='absolute') # 相對絕對位置移動 # cursor.scroll(3,mode='relative') # 相對當前位置移動 res1=cursor.fetchone() res2=cursor.fetchone() res3=cursor.fetchone() res4=cursor.fetchmany(2) res5=cursor.fetchall() print(res1) print(res2) print(res3) print(res4) print(res5) print('%s rows in set (0.00 sec)' %rows) conn.commit() #提交后才發現表中插入記錄成功 cursor.close() conn.close() ''' (1, 'root', '123456') (2, 'root', '123456') (3, 'root', '123456') ((4, 'root', '123456'), (5, 'root', '123456')) ((6, 'root', '123456'), (7, 'lhf', '12356'), (8, 'eee', '156')) rows in set (0.00 sec) '''
(5) 獲取插入的最后一條資料的自增ID-----cursor.lastrowid
import pymysql conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') cursor=conn.cursor() sql='insert into userinfo(name,password) values("xxx","123");' rows=cursor.execute(sql) print(cursor.lastrowid) #在插入陳述句后查看 conn.commit() cursor.close() conn.close()
參考:
https://zhuanlan.zhihu.com/p/115504709
https://zhuanlan.zhihu.com/p/115504709
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/46586.html
標籤:MySQL
下一篇:超市庫存管理資訊系統的程式
