基於pymysql模塊的增刪改查


上課筆記

重點:(熟練)
多表查詢
創建存儲過程
原生sql
索引原理


pymysql  封裝好的客戶端
cursor 底層就是一個send操作
commit 告訴mysql真的要完成修改操作(不然修改不會生效)
execute 執行
查詢不需要commit

password(‘123’) 可以得到hash,mysql自帶的

sql中--代表注釋 后面都無效

sql注入 不需要賬號,密碼就可登陸網站了
跳過前端篩選,所以在應用程序也要在檢測
怎么解決sql注入問題
popen 看一下

cursor.fetchall() 拿到查詢的結果 從管道里拿值

ctrl /注釋 ctrl shift /解除注釋

視圖不要改,只是為了方便查詢,不需要重復連接

以后視圖輕易不要用,查詢有變動要聯系別人DBA修改,很麻煩,不推薦用,自己的數據庫可以用視圖

delimiter 重新聲明一下sql語句結束標志
結束后需要還原 delimiter;

transaction 事務,交易
但凡遇到轉賬,交易之類需要用

rollback 回滾 遇到commit回滾就是修改后的數據

存儲過程 封裝視圖,觸發器,函數等 直接調用存儲過程名
無參存儲過程
delimiter $$
create procedure p1()
BEGIN
......
END
delimiter ;


delimiter $$
create procedure p1()
BEGIN
declare n int default 1;
while (n<100) do
insert into s1 values(n,concat('egon',n),'male',concat('egon',n,'@163.com'));
set n = n+1;
end while;
......
END
delimiter ;

基於pymysql模塊的增刪改查

1.基於pymysql插入操作(有注釋)
 1 import pymysql  2 
 3 # 首先要建立和數據庫的鏈接
 4 client=pymysql.connect(  5     host='127.0.0.1',  6     port=3306,  7     user='root',  8     password='egon123',  9     database='db6', 10     charset='utf8'
11 ) 12 
13 cursor1=client.cursor() # cursor是游標,執行結果默認返回元組
14 
15 sql='insert into t1 values(1,"egon");'  # 插入sql語句
16 try: 17     res=cursor1.execute(sql)  # 執行sql語句
18     print(res) 19     client.commit()  # 加上commit告訴mysql需要完成插入修改操作,不加插入不會生效。
20 except Exception: # 如果sql語句執行出現異常捕捉到就執行回滾操作,把執行成功的語句都撤銷
21  client.rollback() 22 
23 cursor1.close() # 最后記得關閉游標
24 client.close()  # 關閉連接
基於pymysql執行插入操作

 

2.-- 是mysql語句中注釋

1、sql注入之一:用戶存在,繞過密碼
egon' -- 任意字符

2、sql注入之二:用戶不存在,繞過用戶與密碼
xxx' or 1=1 -- 任意字符  1=1是True

sql注入 不需要賬號,密碼就可登陸網站了
現在網站不讓輸入特殊字符,就是因為這個原因。
跳過前端篩選,所以在應用程序也要在檢測。
3.基於pymysql增刪操作(有注釋)

 1 import pymysql  2 
 3 client=pymysql.connect(  4     host='127.0.0.1',  5     port=3306,  6     user='root',  7     password='egon123',  8     database='db6',  9     charset='utf8'
10 ) 11 
12 cursor=client.cursor() 13 
14 sql='insert into t1 values(3,"alex"),(4,"lxx");'
15 userinfo=[ 16     (3,"alex"), 17     (4,"lxx"), 18     (5,"yxx") 19 ] 20 for user in userinfo: # 插入操作
21     sql='insert into t1 values(%s,"%s");' %(user[0],user[1]) 22     # print(sql)
23     cursor.execute(sql)  # 返回的是執行成功的行數
24 
25 sql='insert into t1 values(%s,%s);'
26 cursor.executemany(sql,userinfo)  # 插入多條記錄,它的運行原理就是循環userinfo。
27 
28 cursor.execute('delete from t1 where id=3;')  # 刪除操作
29 
30 client.commit()  # 這個想要修改成功必須要加
31 
32 cursor.close() 33 client.close()
基於pymysql增刪操作

 

4.基於pymysql在數據庫中進行查詢

4.1 存在注入問題的登陸

 1 import pymysql  2 
 3 client=pymysql.connect(  4     host='127.0.0.1',  5     port=3306,  6     user='root',  7     password='egon123',  8     database='db6',  9     charset='utf8'
10 ) 11 
12 cursor=client.cursor() 13 #查詢
14 inp_user=input('輸入賬號名: ').strip() 15 inp_pwd=input('輸入密碼: ').strip() 16 # 在sql語句里password(字符串) 執行hash操作,一般我們不用,都會調用hashlib來完成
17 # sql='select id from user where name = "%s" and pwd = password("%s");' %(inp_user,inp_pwd)
18 sql='select id from user where name = "%s" and pwd = "%s";' %(inp_user,inp_pwd) 19 print(sql) 20 rows=cursor.execute(sql)  # 返回的是行數,執行成功rows就不為0,if rows就為True
21 if rows: # rows返回值不為0,就顯示登陸成功
22     print('\033[45m登陸成功\033[0m') 23 else: 24     print('\033[46m用戶名或密碼錯誤\033[0m') 25 
26 cursor.close() # 日常兩個關閉操作
27 client.close()
存在注入問題

 

4.2 解決注入問題(有注釋)

 1 import pymysql  2 
 3 client=pymysql.connect(  4     host='127.0.0.1',  5     port=3306,  6     user='root',  7     password='egon123',  8     database='db6',  9     charset='utf8'
10 ) 11 
12 cursor=client.cursor() 13 #查詢
14 inp_user=input('輸入賬號名: ').strip() 15 inp_pwd=input('輸入密碼: ').strip() 16 
17 # 放到cursor.execute()里,sql語句會把%s自動識別為字符串,不需要再加引號
18 sql='select id from user where name = %s and pwd = %s;'
19 rows=cursor.execute(sql,(inp_user,inp_pwd)) 20 if rows: 21     print('\033[45m登陸成功\033[0m') 22 else: 23     print('\033[46m用戶名或密碼錯誤\033[0m') 24 
25 cursor.close() 26 client.close()
解決注入問題

 

4.3 獲取查詢結果

 1 # 提交查詢語句並且拿到查詢結果
 2 import pymysql  3 
 4 client=pymysql.connect(  5     host='127.0.0.1',  6     port=3306,  7     user='root',  8     password='egon123',  9     database='db6', 10     charset='utf8'
11 ) 12 
13 cursor=client.cursor(pymysql.cursors.DictCursor) 14 #查詢
15 
16 sql='select * from user where id > 3'
17 rows=cursor.execute(sql) 18 print(rows) 19 print(cursor.fetchall())  # 獲得所有的記錄
20 print(cursor.fetchall()) 21 
22 print(cursor.fetchone()) # 獲得一個記錄
23 print(cursor.fetchone()) 24 print(cursor.fetchone()) 25 
26 print(cursor.fetchmany(2)) # 獲得多個記錄,括號里可以指定記錄
27 print(cursor.fetchone()) 28 
29 
30 
31 
32 print(cursor.fetchall()) 33 # cursor.scroll(0,mode='absolute') # 絕對位置移動
34 # cursor.scroll(1,mode='absolute') # 絕對位置移動
35 print(cursor.fetchall()) 36 
37 
38 print(cursor.fetchone()) 39 cursor.scroll(2,mode='relative') # 相對當前位置移動
40 print(cursor.fetchone()) 41 
42 cursor.close() 43 client.close()
獲取查詢結果

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM