1. 程式人生 > >pymysql模塊

pymysql模塊

local mysql的安裝 userinfo mysq mit 代碼 rowid 關閉 .exe

## pymysql的安裝
pip3 install pymysql

## pymysql的鏈接、執行sql、關閉(遊標)


import pymysql
user=input(用戶名: ).strip()
pwd=input(密碼: ).strip()

#鏈接
conn=pymysql.connect(
    host=localhost,
    user=root,
    database=db3,
    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(登錄失敗) ``` ## execute()sql註入 註意:符號--會註釋掉它之後的sql,正確的語法:--後至少有一個任意字符 根本原理:就根據程序的字符串拼接name
=%s,我們輸入一個_**xxx -- haha**_,用我們輸入的xxx加在程序中拼接成一個判斷條件name=_**xxx -- haha**_``` 最後那一個空格,在一條sql語句中如果遇到select * from t1 where id > 3 -- and name=alex;則--之後的條件被註釋掉了 #1、sql註入之:用戶存在,繞過密碼 alex -- 任意字符 #2、sql註入之:用戶不存在,繞過用戶與密碼 xxx or 1=1 -- 任意字符 ``` 繞過密碼進行登陸 ![](index_files/a46ff1a9-5da8-479d-b1fa-dfd9d526c9a7.png) 繞過用戶和密碼登陸 ![](index_files
/7bbe0232-716c-4244-ad8f-8959c9a4f526.png) 解決方法: ``` # 原來是我們對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的規矩來。 ``` 示例代碼: ```python #!/usr/bin/env python3 # -*- coding:utf-8 -*- import pymysql user = input(>>:).strip() pwd = input(>>:).strip() conn = pymysql.connect( host = 127.0.0.1, port = 3306, user = root, db = db3, charset = utf8 ) cursor = conn.cursor() sql = select * from userinfo where name = %s and password = %s print(sql) rows = cursor.execute(sql,[user,pwd]) print(rows) cursor.close() conn.close() if rows: print(登陸成功) else: print(登陸失敗) ``` ## pymysql 增、刪、改conn.commit() ```python #!/usr/bin/env python3 # -*- coding:utf-8 -*- import pymysql #建立鏈接 conn = pymysql.connect( host = 127.0.0.1, port = 3306, user = root, db = db3, charset = utf8 ) # 拿遊標 cursor = conn.cursor() #執行sql語句 #增加 sql = insert into userinfo(name,password) values(%s,%s); # #刪除 # # sql = ‘delete from userinfo where id = 5;‘ # #修改 # sql = ‘update userinfo set name = "ALEX" where id = 1; ‘ print(sql) # rows = cursor.execute(sql,(‘ab‘,‘123‘)) # 增加一條 rows = cursor.executemany(sql,[(ab,123),(a,123),(b,234)]) # 增加多條 print(cursor.lastrowid) # 獲取最後一個插入的id conn.commit() # 提交後才插入表格中 cursor.close() conn.close() ``` ## **查:fetchone,fetchmany,fetchall** ```python #!/usr/bin/env python3 # -*- coding:utf-8 -*- import pymysql #建立鏈接 conn = pymysql.connect( host = 127.0.0.1, port = 3306, user = root, db = db3, charset = utf8 ) # 拿遊標 cursor = conn.cursor() #執行sql sql = select * from userinfo; rows = cursor.execute(sql)#執行sql語句,返回sql影響成功的行數rows,將結果放入一個集合,等待被查詢 # cursor.scroll(2,mode=‘absolute‘) # 絕對位置移動 # cursor.scroll(2,mode=‘relative‘)# 相對位置移動 res1 = cursor.fetchone() res2 = cursor.fetchone() res3 = cursor.fetchone() res4 = cursor.fetchone() res5 = cursor.fetchmany(2) res = cursor.fetchall() print(res1) print(res2) print(res3) print(res4) print(res5) print(res) print(%s rows in set(0.00 sec)%(rows))

pymysql模塊