一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python中操作mysql的pymysql模塊詳解

Python中操作mysql的pymysql模塊詳解

2020-09-07 08:10蒼松 Python

這篇文章給大家演示了如何安裝以及使用Python中操作mysql的pymysql模塊,本文介紹的很詳細,對大家學習Python具有一定參考借鑒價值,有需要的朋友們一起來看看吧。

前言

pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同。但目前pymysql支持python3.x而后者不支持3.x版本。

本文測試python版本:2.7.11。mysql版本:5.6.24

一、安裝

?
1
pip3 install pymysql

二、使用操作

1、執行SQL

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env pytho
# -*- coding:utf-8 -*-
import pymysql
 
# 創建連接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1', charset='utf8')
# 創建游標
cursor = conn.cursor()
 
# 執行SQL,并返回收影響行數
effect_row = cursor.execute("select * from tb7")
 
# 執行SQL,并返回受影響行數
#effect_row = cursor.execute("update tb7 set pass = '123' where nid = %s", (11,))
 
# 執行SQL,并返回受影響行數,執行多次
#effect_row = cursor.executemany("insert into tb7(user,pass,licnese)values(%s,%s,%s)", [("u1","u1pass","11111"),("u2","u2pass","22222")])
 
 
# 提交,不然無法保存新建或者修改的數據
conn.commit()
 
# 關閉游標
cursor.close()
# 關閉連接
conn.close()

注意:存在中文的時候,連接需要添加charset='utf8',否則中文顯示亂碼。

2、獲取查詢數據

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
cursor = conn.cursor()
cursor.execute("select * from tb7")
 
# 獲取剩余結果的第一行數據
row_1 = cursor.fetchone()
print row_1
# 獲取剩余結果前n行數據
# row_2 = cursor.fetchmany(3)
 
# 獲取剩余結果所有數據
# row_3 = cursor.fetchall()
 
conn.commit()
cursor.close()
conn.close()

3、獲取新創建數據自增ID

可以獲取到最新自增的ID,也就是最后插入的一條數據ID

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
cursor = conn.cursor()
effect_row = cursor.executemany("insert into tb7(user,pass,licnese)values(%s,%s,%s)", [("u3","u3pass","11113"),("u4","u4pass","22224")])
conn.commit()
cursor.close()
conn.close()
#獲取自增id
new_id = cursor.lastrowid     
print new_id

4、移動游標

操作都是靠游標,那對游標的控制也是必須的

?
1
2
3
4
注:在fetch數據時按照順序進行,可以使用cursor.scroll(num,mode)來移動游標位置,如:
 
cursor.scroll(1,mode='relative') # 相對當前位置移動
cursor.scroll(2,mode='absolute') # 相對絕對位置移動

 

5、fetch數據類型

關于默認獲取的數據是元祖類型,如果想要或者字典類型的數據,即:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
#游標設置為字典類型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select * from tb7")
 
row_1 = cursor.fetchone()
print row_1  #{u'licnese': 213, u'user': '123', u'nid': 10, u'pass': '213'}
 
conn.commit()
cursor.close()
conn.close()

6、調用存儲過程

a、調用無參存儲過程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
 
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
#游標設置為字典類型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
#無參數存儲過程
cursor.callproc('p2'#等價于cursor.execute("call p2()")
 
row_1 = cursor.fetchone()
print row_1
 
 
conn.commit()
cursor.close()
conn.close()

b、調用有參存儲過程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
 
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
 
cursor.callproc('p1', args=(1, 22, 3, 4))
#獲取執行完存儲的參數,參數@開頭
cursor.execute("select @p1,@_p1_1,@_p1_2,@_p1_3"#{u'@_p1_1': 22, u'@p1': None, u'@_p1_2': 103, u'@_p1_3': 24}
row_1 = cursor.fetchone()
print row_1
 
 
conn.commit()
cursor.close()
conn.close()

三、關于pymysql防注入

 1、字符串拼接查詢,造成注入

正常查詢語句:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
cursor = conn.cursor()
user="u1"
passwd="u1pass"
#正常構造語句的情況
sql="select user,pass from tb7 where user='%s' and pass='%s'" % (user,passwd)
#sql=select user,pass from tb7 where user='u1' and pass='u1pass'
row_count=cursor.execute(sql) row_1 = cursor.fetchone()
print row_count,row_1
 
conn.commit()
cursor.close()
conn.close()

構造注入語句:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
cursor = conn.cursor()
 
user="u1' or '1'-- "
passwd="u1pass"
sql="select user,pass from tb7 where user='%s' and pass='%s'" % (user,passwd)
 
#拼接語句被構造成下面這樣,永真條件,此時就注入成功了。因此要避免這種情況需使用pymysql提供的參數化查詢。
#select user,pass from tb7 where user='u1' or '1'-- ' and pass='u1pass'
 
row_count=cursor.execute(sql)
row_1 = cursor.fetchone()
print row_count,row_1
 
 
conn.commit()
cursor.close()
conn.close()

 

 2、避免注入,使用pymysql提供的參數化語句

正常參數化查詢

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
 
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
cursor = conn.cursor()
user="u1"
passwd="u1pass"
#執行參數化查詢
row_count=cursor.execute("select user,pass from tb7 where user=%s and pass=%s",(user,passwd))
row_1 = cursor.fetchone()
print row_count,row_1
 
conn.commit()
cursor.close()
conn.close()

構造注入,參數化查詢注入失敗。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
cursor = conn.cursor()
 
user="u1' or '1'-- "
passwd="u1pass"
#執行參數化查詢
row_count=cursor.execute("select user,pass from tb7 where user=%s and pass=%s",(user,passwd))
#內部執行參數化生成的SQL語句,對特殊字符進行了加\轉義,避免注入語句生成。
# sql=cursor.mogrify("select user,pass from tb7 where user=%s and pass=%s",(user,passwd))
# print sql
#select user,pass from tb7 where user='u1\' or \'1\'-- ' and pass='u1pass'被轉義的語句。
 
row_1 = cursor.fetchone()
print row_count,row_1
 
conn.commit()
cursor.close()
conn.close()

結論:excute執行SQL語句的時候,必須使用參數化的方式,否則必然產生SQL注入漏洞。

3、使用存mysql儲過程動態執行SQL防注入

使用MYSQL存儲過程自動提供防注入,動態傳入SQL到存儲過程執行語句。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
delimiter \\
DROP PROCEDURE IF EXISTS proc_sql \\
CREATE PROCEDURE proc_sql (
  in nid1 INT,
  in nid2 INT,
  in callsql VARCHAR(255)
  )
BEGIN
  set @nid1 = nid1;
  set @nid2 = nid2;
  set @callsql = callsql;
    PREPARE myprod FROM @callsql;
--   PREPARE prod FROM 'select * from tb2 where nid>? and nid<?';  傳入的值為字符串,?為占位符
--   用@p1,和@p2填充占位符
    EXECUTE myprod USING @nid1,@nid2;
  DEALLOCATE prepare myprod;
 
END\\
delimiter ;
?
1
2
3
4
set @nid1=12;
set @nid2=15;
set @callsql = 'select * from tb7 where nid>? and nid<?';
CALL proc_sql(@nid1,@nid2,@callsql)

pymsql中調用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
cursor = conn.cursor()
mysql="select * from tb7 where nid>? and nid<?"
cursor.callproc('proc_sql', args=(11, 15, mysql))
 
rows = cursor.fetchall()
print rows #((12, 'u1', 'u1pass', 11111), (13, 'u2', 'u2pass', 22222), (14, 'u3', 'u3pass', 11113))
conn.commit()
cursor.close()
conn.close()

四、使用with簡化連接過程

每次都連接關閉很麻煩,使用上下文管理,簡化連接過程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "TKQ"
 
import pymysql
import contextlib
#定義上下文管理器,連接后自動關閉連接
@contextlib.contextmanager
def mysql(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1',charset='utf8'):
  conn = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db, charset=charset)
  cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
  try:
    yield cursor
  finally:
    conn.commit()
    cursor.close()
    conn.close()
 
# 執行sql
with mysql() as cursor:
  print(cursor)
  row_count = cursor.execute("select * from tb7")
  row_1 = cursor.fetchone()
  print row_count, row_1

總結

以上就是關于Python中pymysql模塊的全部內容,希望對大家學習或使用python能有一定的幫助,如果有疑問大家可以留言交流。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色噜噜 男人的天堂在线观看 | 日本花季传媒2020旧版安卓 | 给我免费观看的视频在线播放 | 奇米小说 | 出轨同学会免费观看 | 日韩欧美亚洲每日更新网 | 国产第一页无线好源 | 毛片资源站 | 日韩精品免费一区二区三区 | 91chinese 永久免费 | 亚洲黄网站wwwwww | 67194久久 | 四虎影院最新网址 | 日本xx高清视频免费观看 | 无限好资源第一片免费韩国 | 九九精品成人免费国产片 | 亚洲品质自拍视频 | 欧美xxxxx九色视频免费观看 | 欧美最猛性xxxxx69交 | 爱爱小说漫画 | 亚飞与亚基高清国语在线观看 | 冰雪奇缘1完整版免费观看 变形金刚第一部 | 亚洲免费小视频 | 全肉np巨肉一女np高h双龙 | 亚洲第一免费播放区 | 第四色男人天堂 | 亚洲视频在线免费观看 | narutotsunade全彩雏田 | 国产99视频精品免视看9 | 午夜精品久久久内射近拍高清 | 日日摸夜夜爽色婷婷91 | 国内视频一区二区三区 | 日韩永久在线观看免费视频 | 国产日产精品久久久久快鸭 | 欧美亚洲欧美 | 国产videos hd | 日韩在线天堂免费观看 | 女人叉开腿让男人捅 | 欧美视频在线一区 | tobu8中国在线播放免费 | 好大好硬好深好爽想要之黄蓉 |