Python(PyMySQL模塊)

day60html

參考:http://www.cnblogs.com/wupeiqi/articles/5713330.htmlmysql

查詢(登陸)s1.pylinux

 1 import pymysql  2 
 3 user = input("username:")  4 pwd = input("password:")  5 
 6 #鏈接數據庫
 7 
 8 #打開
 9 conn = pymysql.connect(host= "localhost", user = 'root', password='112358', database = 'db4') 10 #
11 cursor = conn.cursor() 12 
13 sql = "select * from userinfo where username = '%s' and password = '%s'" %(user, pwd) 14 # select * from userinfo where username='uu' or 1=1 -- ' and password='%s'
15 #其中 1=1百分百成立( -- )將後半部分註釋
16 #uu' or 1=1 -- 
17 # 倪志鵬' -- 也行
18 cursor.execute(sql) 19 result = cursor.fetchone() #
20 
21 #關閉數據庫
22 cursor.close() 23 conn.close() 24 
25 if result: 26     print('登陸成功') 27 else: 28     print('登陸失敗')

執行結果:sql

username:古麗 password:123456 登陸成功 Process finished with exit code 0

 

可是以上方式存在一個問題:數據庫

不用用戶名和密碼也能登陸。fetch

是由於SQL注入。spa

sql = "select * from userinfo where username = '%s' and password = '%s'" %(user, pwd) # select * from userinfo where username='uu' or 1=1 -- ' and password='%s' #其中 1=1百分百成立( -- )將後半部分註釋 #uu' or 1=1 --  # 倪志鵬' -- 也行

若是用拼接方式存在漏洞,or 1 = 1,百分百處理, -- 會將後半部分註釋,因此無所謂輸入的密碼是什麼。code

 

應改成如下方式s2.py:htm

 1 import pymysql  2 
 3 user = input("username:")  4 pwd = input("password:")  5 
 6 #鏈接數據庫
 7 
 8 #打開
 9 conn = pymysql.connect(host= "localhost", user = 'root', password='112358', database = 'db4') 10 #
11 cursor = conn.cursor() 12 
13 #不要字符串拼接
14 sql = "select * from userinfo where username = %s and password = %s"
15 
16 #cursor.execute(sql, user, pwd) linux下報錯
17 
18 cursor.execute(sql, [user, pwd]) 19 
20 result = cursor.fetchone() #
21 
22 #關閉數據庫
23 cursor.close() 24 conn.close() 25 
26 if result: 27     print('登陸成功') 28 else: 29     print('登陸失敗')

其中第16行在linux下可能出錯。blog

執行結果

username:asc password:12313 登陸成功 Process finished with exit code 0

 

插入 s3.py

 1 import pymysql  2 
 3 user = "呱呱"
 4 pwd = "asca"
 5 
 6 #鏈接數據庫
 7 #打開
 8 conn = pymysql.connect(host= "localhost", user = 'root', password='112358', database = 'db4')  9 #
10 cursor = conn.cursor() 11 #
12 # sql = "insert into userinfo(username, password) values(%s,%s)"
13 # cursor.execute(sql,[user,pwd])
14 
15 #增長多個
16 sql = "insert into userinfo(username, password) values(%s,%s)"
17 r = cursor.executemany(sql,[('dssdsa', '21131'), ('asc', '12313')]) 18 #其中r爲受影響的行數
19 conn.commit()#提交
20 
21 
22 #關閉數據庫
23 cursor.close() 24 conn.close()

16~17爲插入多對數據,其中r爲受影響的行數。

19行不要忘了提交。

 

取fetchone、fetchmany等 s4.py

 1 import pymysql  2 
 3 #打開
 4 conn = pymysql.connect(host= "localhost", user = 'root', password='112358', database = 'db4')  5 #
 6 cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)#帶其中參數,可以使結果爲列表帶字典
 7 
 8 sql = "select * from userinfo;"
 9 
10 cursor.execute(sql) 11 result = cursor.fetchone() #每次拿一個
12 print(result) 13 result = cursor.fetchone() #
14 print(result) 15 
16 #按順序拿
17 result = cursor.fetchmany(2) #
18 print(result)#元組
19 
20 #關閉數據庫
21 cursor.close() 22 conn.close()

按順序拿取,其中第6行帶參數,能夠輸出列表帶字典,不帶參數輸出元組。

執行結果:

{'username': '古麗', 'id': 2, 'password': '123456'} {'username': 'dssdsa', 'id': 8, 'password': '21131'} [{'username': 'asc', 'id': 9, 'password': '12313'}, {'username': 'dssdsa', 'id': 10, 'password': '21131'}] Process finished with exit code 0

 

新插入數據的自增id s5.py
 1 #新插入數據的自增id
 2 
 3 #文章表:id title hobby_count
 4 # 1 assda 0
 5 
 6 import pymysql  7 
 8 #鏈接數據庫
 9 #打開
10 conn = pymysql.connect(host= "localhost", user = 'root', password='112358', database = 'db4') 11 #
12 cursor = conn.cursor() 13 #
14 sql = "insert into userinfo(username, password) values('哈哈','000000')"
15 cursor.execute(sql)  #linux下報錯
16 conn.commit()#提交
17 print(cursor.lastrowid)#輸出插入語句的自增id
18 
19 #關閉數據庫
20 cursor.close() 21 conn.close()

第17行輸出插入語句的自增id。

輸出結果:

16 Process finished with exit code 0

相關文章
相關標籤/搜索