pymysql操作数据库 - Go语言中文社区

pymysql操作数据库


转自:

https://www.cnblogs.com/wt11/p/6141225.html

https://www.cnblogs.com/jl-bai/archive/2016/12/02/6124088.html

一.简介 

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

  其执行语句与sql源码相似

二.使用

1.安装

   pip install pymysql

2.使用操作

  先来一例完整的连接加基本的操作

复制代码

import pymysql
  
# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
#第二种创建连接方式
#连接配置信息 
config = { 
    'host':'127.0.0.1', 
    'port':3306, 
    'user':'root', 
    'password':'123', 
    'db':'t1', 
    'charset':'utf8mb4',         
    'cursorclass':pymysql.cursors.DictCursor, 
} 
# 创建连接 (2)
connection = pymysql.connect(**config)

# 创建游标
cursor = conn.cursor()
  
# 执行SQL,并返回收影响行数
effect_row = cursor.execute("update hosts set host = '1.1.1.2'")
  
# 执行SQL,并返回受影响行数
#effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,))
  
# 执行SQL,并返回受影响行数
#effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])
  
  
# 提交,不然无法保存新建或者修改的数据
conn.commit()
  
# 关闭游标
cursor.close()
# 关闭连接
conn.close()    

复制代码

向数据库插入数据,使用try语句,当出现异常是主动回滚

复制代码

#!/usr/bin/python3

import pymysql

# 打开数据库连接
db = pymysql.connect("localhost","testuser","test123","TESTDB" )

# 使用cursor()方法获取操作游标 
cursor = db.cursor()

# SQL 插入语句
sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
         LAST_NAME, AGE, SEX, INCOME)
         VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
try:
   # 执行sql语句
   cursor.execute(sql)
   # 提交到数据库执行
   db.commit()
except:
   # 如果发生错误则回滚
   db.rollback()

# 关闭数据库连接
db.close()

复制代码

3.向数据表中插入多条数据,使用executemany方法,在生产环境中插入多条数据 ,在后台中获取数据后,以列表的形式传入语句([('v1','v2'),('v3','v4')])

复制代码

# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
# 创建游标
cur = conn.cursor()
 if request.method == "POST":
        title = request.POST.get("title")
        title_en = request.POST.get("title_en")
        content = request.POST.get("content")
        content_en = request.POST.get("content_en")
        notification_type =request.POST.get("notification_type").strip()
        user_list = request.POST.get("user_list")
        updated_datetime = datetime.now()
        created_datetime = datetime.now()
        values_list = []
         for user in user_id_list:
                temp = updated_datetime,created_datetime,title,title_en,content,content_en,notification_type,user['id']
                values_list.append((temp))
     try:
          cur.executemany('''insert into app_notification(updated_datetime, created_datetime, title, title_en,
                                  content, content_en, notification_type, is_read, recipient_id)
                      values(%s, %s, %s, %s, %s, %s, %s, 0, %s)''',values_list)
            conn.commit()
            conn.close()
     
    except Exception as err:
        conn.rollback()
        logging.error(err)
        logging.error(traceback.format_exc())
        conn.close()

复制代码

# 获取最新自增ID

  new_id = cursor.lastrowid

4.数据库查询操作

Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。

  • fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
  • fetchall(): 接收全部的返回结果行.
  • rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。

复制代码

import pymysql
  
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
cursor = conn.cursor()
cursor.execute("select * from hosts")
  
# 获取第一行数据
row_1 = cursor.fetchone()
  
# 获取前n行数据
# row_2 = cursor.fetchmany(3)
# 获取所有数据
# row_3 = cursor.fetchall()
  
conn.commit()
cursor.close()
conn.close()

复制代码

注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:

  • cursor.scroll(1,mode='relative')  # 相对当前位置移动
  • cursor.scroll(2,mode='absolute') # 相对绝对位置移动

5。fetch数据类型

  关于默认获取的数据是元祖类型,如果想要或者字典类型的数据,即:

复制代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pymysql
  
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
  
# 游标设置为字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
r = cursor.execute("call p1()")
  
result = cursor.fetchone()
  
conn.commit()
cursor.close()
conn.close()

复制代码

错误处理

DB API中定义了一些数据库操作的错误及异常,下表列出了这些错误和异常:

异常 描述
Warning 当有严重警告时触发,例如插入数据是被截断等等。必须是 StandardError 的子类。
Error 警告以外所有其他错误类。必须是 StandardError 的子类。
InterfaceError 当有数据库接口模块本身的错误(而不是数据库的错误)发生时触发。 必须是Error的子类。
DatabaseError 和数据库有关的错误发生时触发。 必须是Error的子类。
DataError 当有数据处理时的错误发生时触发,例如:除零错误,数据超范围等等。 必须是DatabaseError的子类。
OperationalError 指非用户控制的,而是操作数据库时发生的错误。例如:连接意外断开、 数据库名未找到、事务处理失败、内存分配错误等等操作数据库是发生的错误。 必须是DatabaseError的子类。
IntegrityError 完整性相关的错误,例如外键检查失败等。必须是DatabaseError子类。
InternalError 数据库的内部错误,例如游标(cursor)失效了、事务同步失败等等。 必须是DatabaseError子类。
ProgrammingError 程序错误,例如数据表(table)没找到或已存在、SQL语句语法错误、 参数数量错误等等。

 

 

前言

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

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zhaomax/article/details/88045109
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:41:18
  • 阅读 ( 1267 )
  • 分类:数据库

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢