mysql学习【第14篇】:pymysql
生活随笔
收集整理的這篇文章主要介紹了
mysql学习【第14篇】:pymysql
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
pymysql模塊
database='day47',charset='utf8') cursor=conn.cursor() #拿到游標(biāo),即mysql > #執(zhí)行sql sql='select * from user where user="%s" and password="%s";'%(user,pwd) print(sql) #注意%s需要加雙引號 rows = cursor.execute(sql) #拿到受影響的行數(shù) cursor.close() conn.close() if rows: print('登錄成功') else: print('登錄失敗')
from t1 where id > 3 -- and name='egon';則--之后的條件被注釋掉了#1、sql注入之:用戶存在,繞過密碼 egon' -- 任意字符#2、sql注入之:用戶不存在,繞過用戶與密碼 xxx' or 1=1 -- 任意字符
and password=%s" #!!!注意%s需要去掉引號,因為pymysql會自動為我們加上 rows=cursor.execute(sql,[user,pwd])
#pymysql模塊自動幫我們解決sql注入的問題,只要我們按照pymysql的規(guī)矩來。
database='day47',charset='utf8') cursor=conn.cursor()sql='insert into user1(user,password) values(%s,%s);' rows=cursor.execute(sql,('alex','123')) # rows=cursor.executemany(sql,[('yuanhao','123'),('laowu','123'),('kgf','12323')]) conn.commit() print(cursor.lastrowid) #查看表中最后一行的iD cursor.close() conn.close()
一、安裝的兩種方法
第一種
#安裝 pip3 install pymysql第二種
二、鏈接,執(zhí)行sql,關(guān)閉(游標(biāo))
import pymysql user= input('用戶名:>>').strip() pwd= input('密碼:>>').strip()#先鏈接,拿到游標(biāo) conn=pymysql.connect(host='localhost',user='root',password='123456',database='day47',charset='utf8') cursor=conn.cursor() #拿到游標(biāo),即mysql > #執(zhí)行sql sql='select * from user where user="%s" and password="%s";'%(user,pwd) print(sql) #注意%s需要加雙引號 rows = cursor.execute(sql) #拿到受影響的行數(shù) cursor.close() conn.close() if rows: print('登錄成功') else: print('登錄失敗')
?
三、execute()之sql注入
注意:符號--會注釋掉它之后的sql,正確的語法:--后至少有一個任意字符
根本原理:就根據(jù)程序的字符串拼接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進(jìn)行字符串拼接 # sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd) # print(sql) # rows=cursor.execute(sql)#改寫為(execute幫我們做字符串拼接,我們無需且一定不能再為%s加引號了) sql="select * from userinfo where name=%sand password=%s" #!!!注意%s需要去掉引號,因為pymysql會自動為我們加上 rows=cursor.execute(sql,[user,pwd])
#pymysql模塊自動幫我們解決sql注入的問題,只要我們按照pymysql的規(guī)矩來。
四、增、刪、改:conn.commit()
增:
import pymysql 先鏈接,拿到游標(biāo) conn=pymysql.connect(host='localhost',user='root',password='123456',database='day47') cursor=conn.cursor() #拿到游標(biāo),即mysql > #執(zhí)行sql 增: sql='insert into user1(user,password) VALUES (%s,%s)' print(sql) # rows = cursor.execute(sql,('xixi',123)) #插入一條記錄 rows = cursor.executemany(sql,[('xixi',123),('aaa',456),('ttt',147)]) #插入多行記錄 print('%s row in set (0.00 sec)'%rows)conn.commit() #提交到數(shù)據(jù)庫 cursor.close() conn.close()刪:
import pymysql #先鏈接,拿到游標(biāo) name=input('>>').strip() conn=pymysql.connect(host='localhost',user='root',password='123456',database='day47') cursor=conn.cursor() #拿到游標(biāo),即mysql > #執(zhí)行sql 刪: sql='delete from user1 where user =%s;' #刪除數(shù)據(jù) print(sql) rows = cursor.execute(sql,(name)) print('%s row in set (0.00 sec)'%rows) conn.commit() #提交到數(shù)據(jù)庫 cursor.close() conn.close()改:
import pymysql #先鏈接,拿到游標(biāo) id=input('>>').strip() conn=pymysql.connect(host='localhost',user='root',password='123456',database='day47') cursor=conn.cursor() #拿到游標(biāo),即mysql > #執(zhí)行sql 改: sql=' update user1 set password = "5555555" where id=%s;' print(sql) rows = cursor.execute(sql,(id)) print('%s row in set (0.00 sec)'%rows) conn.commit() #提交到數(shù)據(jù)庫 cursor.close() conn.close()五、查:fetchone,fetchmany,fetchall
---------查fetchone,fetchmany,fetchall----------- import pymysql conn=pymysql.connect(host='localhost',user='root',password='123456',database='day47') cursor=conn.cursor() #拿到游標(biāo),即mysql > #執(zhí)行sql 查: sql='select * from user1;' rows = cursor.execute(sql)#查單條fetchone res1=cursor.fetchone() res2=cursor.fetchone() res3=cursor.fetchone() print(res1) print(res2) print(res3) print(res3[0]) #查多條fetchmany print(cursor.fetchmany(3)) print(cursor.fetchone()) #查所有fetchall print(cursor.fetchall()) print(cursor.fetchone()) #-------光標(biāo)的移動-------- #1.絕對路徑:從文件的開頭位置算起 print(cursor.fetchall()) cursor.scroll(1,mode='absolute') print(cursor.fetchone()) cursor.scroll(3,mode='absolute') print(cursor.fetchone()) #2.相對路徑: print(cursor.fetchone()) print(cursor.fetchone()) cursor.scroll(2,mode='relative') #相對于上面的兩條向后移兩條 print(cursor.fetchone()) print('%s row in set (0.00 sec)' %rows) cursor.close() conn.close()六、獲取插入的最后一條數(shù)據(jù)的自增ID
------查看表中最后一行的iD import pymysql conn=pymysql.connect(host='localhost',user='root',password='123456',database='day47',charset='utf8') cursor=conn.cursor()sql='insert into user1(user,password) values(%s,%s);' rows=cursor.execute(sql,('alex','123')) # rows=cursor.executemany(sql,[('yuanhao','123'),('laowu','123'),('kgf','12323')]) conn.commit() print(cursor.lastrowid) #查看表中最后一行的iD cursor.close() conn.close()
?
轉(zhuǎn)載于:https://www.cnblogs.com/kcwxx/p/10145595.html
總結(jié)
以上是生活随笔為你收集整理的mysql学习【第14篇】:pymysql的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用随机数以及扩容表进行join代码
- 下一篇: Android组件化专题 - 组件化配置