`

python中操作mysql的pymysql模块详解

阅读更多

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

本文测试python版本:3.5.2。mysql版本:5.7.18

一、安装
pip install pymysql
二、使用操作

1. 执行SQL

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'junxi'

import pymysql

# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')

# 创建游标, 查询数据默认为元组类型
cursor = conn.cursor()


# 执行SQL,并返回收影响行数
row1 = cursor.execute("update users set password = '123'")
print(row1)
# 执行SQL,并返回受影响行数
row2 = cursor.execute("update users set password = '456' where id > %s", (1,))
print(row2)
# 执行SQL,并返回受影响行数(使用pymysql的参数化语句防止SQL注入)
row3 = cursor.executemany("insert into users(username, password, email)values(%s, %s, %s)", [("ceshi3", '333', 'ceshi3@11.com'), ("ceshi4", '444', 'ceshi4@qq.com')])
print(row3)

# 提交,不然无法保存新建或者修改的数据
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

提示:存在中文的时候,连接需要添加charset='utf8',否则中文显示乱码。

2、获取查询数据

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'junxi'

import pymysql

# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')

# 创建游标, 查询数据默认为元组类型
cursor = conn.cursor()
cursor.execute("select * from users")

# 获取第一行数据
row_1 = cursor.fetchone()
print(row_1)
# 获取前n行数据
row_n = cursor.fetchmany(3)
print(row_n)
# 获取所有数据
row_3 = cursor.fetchall()
print(row_3)


# 提交,不然无法保存新建或者修改的数据
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

3、获取新创建数据自增ID
可以获取到最新自增的ID,也就是最后插入的一条数据ID

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'junxi'

import pymysql

# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')

# 创建游标, 查询数据默认为元组类型
cursor = conn.cursor()

cursor.executemany("insert into users(username, password, email)values(%s, %s, %s)", [("ceshi3", '333', 'ceshi3@11.com'), ("ceshi4", '444', 'ceshi4@qq.com')])
new_id = cursor.lastrowid
print(new_id)


# 提交,不然无法保存新建或者修改的数据
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

4、移动游标
操作都是靠游标,那对游标的控制也是必须的

注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:
 
cursor.scroll(1,mode='relative') # 相对当前位置移动
cursor.scroll(2,mode='absolute') # 相对绝对位置移动

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

import pymysql

# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')

# 游标设置为字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 左连接查询
r = cursor.execute("select * from users as u left join articles as a on u.id = a.user_id where a.user_id = 2")
result = cursor.fetchall()
print(result)

# 查询一个表的所有字段名
c = cursor.execute("SHOW FULL COLUMNS FROM users FROM blog")
cc = cursor.fetchall()


# 提交,不然无法保存新建或者修改的数据
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

查看运行结果:

[{'user_id': 2, 'id': 2, 'password': '456', 'email': 'xinlei2017@test.com', 'a.id': 2, 'content': '成名之路', 'title': '星光大道', 'username': 'tangtang'}]

6、调用存储过程

a、调用无参存储过程

#! /usr/bin/env python
# -*- coding:utf-8 -*-

import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')
#游标设置为字典类型
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、调用有参存储过程

#! /usr/bin/env python
# -*- coding:utf-8 -*-

import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')
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、字符串拼接查询,造成注入

正常查询语句:

#! /usr/bin/env python
# -*- coding:utf-8 -*-

import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')
cursor = conn.cursor()
username = "ceshi1"
password = "ceshi1passwd"
# 正常构造语句的情况
sql = "select username, password from users where user='%s' and pass='%s'" % (username, password)
# sql = select username, password from users where user='ceshi1' and pass='ceshi1passwd'
row_count = cursor.execute(sql) 
row_1 = cursor.fetchone()
print row_count, row_1
 
conn.commit()
cursor.close()
conn.close()

构造注入语句:

#! /usr/bin/env python
# -*- coding:utf-8 -*-

import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')
cursor = conn.cursor()
 
username = "u1' or '1'-- "
password = "u1pass"
sql="select username, password from users where username='%s' and password='%s'" % (username, password)
 
# 拼接语句被构造成下面这样,永真条件,此时就注入成功了。因此要避免这种情况需使用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提供的参数化语句
正常参数化查询

#! /usr/bin/env python
# -*- coding:utf-8 -*-

 
import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')
cursor = conn.cursor()
username="u1"
password="u1pass"
#执行参数化查询
row_count=cursor.execute("select username,password from tb7 where username=%s and password=%s",(username,password))
row_1 = cursor.fetchone()
print row_count,row_1
 
conn.commit()
cursor.close()
conn.close()

构造注入,参数化查询注入失败。

#! /usr/bin/env python
# -*- coding:utf-8 -*-

import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')
cursor = conn.cursor()
 
username="u1' or '1'-- "
password="u1pass"
#执行参数化查询
row_count=cursor.execute("select username,password from users where username=%s and password=%s",(username,password))
#内部执行参数化生成的SQL语句,对特殊字符进行了加\转义,避免注入语句生成。
# sql=cursor.mogrify("select username,password from users where username=%s and password=%s",(username,password))
# print sql
#select username,password from users where username='u1\' or \'1\'-- ' and password='u1pass'被转义的语句。
 
row_1 = cursor.fetchone()
print row_count,row_1
 
conn.commit()
cursor.close()
conn.close()

结论:excute执行SQL语句的时候,必须使用参数化的方式,否则必然产生SQL注入漏洞。

3、使用存mysql储过程动态执行SQL防注入

使用MYSQL存储过程自动提供防注入,动态传入SQL到存储过程执行语句。
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 users where nid>? and nid<?';  传入的值为字符串,?为占位符
--   用@p1,和@p2填充占位符
    EXECUTE myprod USING @nid1,@nid2;
  DEALLOCATE prepare myprod;
 
END\\
delimiter ;
set @nid1=12;
set @nid2=15;
set @callsql = 'select * from users where nid>? and nid<?';
CALL proc_sql(@nid1,@nid2,@callsql)

pymsql中调用

#! /usr/bin/env python
# -*- coding:utf-8 -*-

import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', charset='utf8')
cursor = conn.cursor()
sql1="select * from users where nid>? and nid<?"
cursor.callproc('proc_sql', args=(11, 15, sql1))
 
rows = cursor.fetchall()
print rows
conn.commit()
cursor.close()
conn.close()
四、使用with简化连接过程
# 使用with简化连接过程,每次都连接关闭很麻烦,使用上下文管理,简化连接过程
import pymysql
import contextlib


# 定义上下文管理器,连接后自动关闭连接
@contextlib.contextmanager
def mysql(host='127.0.0.1', port=3306, user='blog', passwd='123456', db='blog', 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:
    # 左连接查询
    r = cursor.execute("select * from users as u left join articles as a on u.id = a.user_id where a.user_id = 2")
    result = cursor.fetchall()
    print(result)

查看运行结果:

[{'title': '星光大道', 'username': 'tangtang', 'user_id': 2, 'email': 'xinlei3166@126.com', 'a.id': 2, 'content': '成名之路', 'password': '456', 'id': 2}]
<!-- -->
 



链接:http://www.jianshu.com/p/f11508c98e62

分享到:
评论

相关推荐

    pymysql适用python2.7版本

    《Python2.7环境下使用PyMySQL进行数据库操作详解》 在Python编程中,与数据库的交互是不可或缺的一部分,尤其是在处理大量数据或构建Web应用程序时。PyMySQL是一个纯Python实现的MySQL客户端库,它为Python2.7及更...

    Python MySQL数据库连接池组件pymysqlpool详解

    pymysqlpool (本地下载)是数据库工具包中新成员,目的是能提供一个实用的数据库连接池中间件,从而避免在应用中频繁地创建和释放数据库连接资源。 功能 连接池本身是线程安全的,可在多线程环境下使用,不必...

    Python中pymysql 模块的使用详解

    Python中的pymysql模块是用于在Python环境下与MySQL数据库交互的工具,它是Python 3.x版本的MySQL数据库接口。在Python 2.x中,通常使用MySQLdb模块,但随着Python 3的普及,pymysql成为了更优选的选项,因为它完全...

    python数据库操作mysql:pymysql、sqlalchemy常见用法详解

    `pymysql`是一个纯Python编写的MySQL数据库驱动,它允许你通过Python代码直接执行SQL语句,类似于在命令行中操作MySQL。在使用`pymysql`之前,需要先通过`pip3 install pymysql`或在PyCharm中安装这个模块。 连接...

    pymssql-2.2.4-cp38-cp38-win_amd64.zip

    《Python中的pymysql模块详解及安装指南》 在Python编程中,数据库操作是一项不可或缺的任务。pymysql作为Python连接MySQL数据库的一个重要模块,为开发者提供了便捷的数据交互接口。本文将详细阐述pymysql模块的...

    python3连接MySQL数据库实例详解

    本文实例为大家分享了python3连接MySQL数据库的具体代码,供大家参考,具体内容如下 #python3连接MySQL实例 import pymysql """导入连接MySQL需要的包,没有安装pymysql需要先安装 使用命令行切换到python的安装...

    PyMySQL 源码安装包

    同时,它还提供了一些高级特性,如处理大结果集、预编译语句、自定义编码等,使得在 Python 中操作 MySQL 更加便捷高效。 总结来说,PyMySQL 是一个功能丰富的 Python MySQL 客户端库,它为 Python 3 提供了与 ...

    Python 操作MySQL详解及实例

    Python 操作MySQL详解及实例 使用Python进行MySQL的库主要有三个,Python-MySQL(更熟悉的名字可能是MySQLdb),PyMySQL和SQLAlchemy。 Python-MySQL资格最老,核心由C语言打造,接口精炼,性能最棒,缺点是环境依赖...

    「Python入门」python操作MySQL和SqlServer

    ### Python 操作 MySQL 数据库详解 #### 一、pymysql 模块介绍与安装 - **pymysql** 是一个用 Python 编写的 MySQL 驱动程序,它允许我们通过 Python 语言来操作 MySQL 数据库。这个库提供了一个简单易用的方式来...

    pymysql模块的操作实例

    _pymysql_ 是Python中用于操作MySQL数据库的一个第三方模块,它提供了与MySQL数据库交互的功能,包括连接、查询、插入、更新和删除等操作。以下将详细介绍如何使用pymysql模块进行数据库操作。 首先,安装pymysql...

    MySQL适配器PyMySQL详解

    在 Python 3.x 中,由于 Python 2.x 的 mysqldb 库不再维护,PyMySQL 成为了首选的 MySQL 连接库。PyMySQL 遵循 Python 数据库 API v2.0 规范,提供了类似于原生 MySQL 客户端的功能,而且完全由 Python 代码编写,...

    关于pymysql模块的使用以及代码详解

    标题中的“pymysql模块的使用以及代码详解”表示本篇文章将要详细解读Python中pymysql模块的使用方法,并对代码实例进行解析。pymysql是一个在Python编程语言中用于连接MySQL数据库的库,它允许Python脚本与MySQL...

    pymysql模块的使用(增删改查)详解

    之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库,那如何在python程序中操作数据库呢?这就用到了pymysql模块,该模块本质就是一个套接字客户端软件,使用前需要事先安装。 (1)pymysql模块的下载 ...

    pymysql-0.6.3

    《Python数据库连接模块PyMySQL-0.6.3详解》 在Python编程中,与数据库进行交互是一项常见的任务。PyMySQL作为Python中的一个开源库,为开发者提供了与MySQL数据库进行连接和操作的强大工具。PyMySQL-0.6.3是这个...

    2020最新_Python_(MySQL_SQL_Redis)数据库详解【千锋】源码+课件+笔记.zip

    本教程“2020最新_Python_(MySQL_SQL_Redis)数据库详解【千锋】源码+课件+笔记.zip”涵盖了这些关键知识点,旨在帮助学习者深入理解和应用Python的数据库操作技术。 首先,让我们关注Python中的MySQL数据库连接。...

    用 Python 连接 MySQL 的几种方式详解1

    无论你是进行数据分析、网络爬虫、Web 开发还是机器学习,掌握如何使用 Python 操作 MySQL 都是非常重要的技能。 1. **MySQL-python/MySQLdb** MySQL-python,也称为 MySQLdb,是 Python 连接 MySQL 的经典库,...

Global site tag (gtag.js) - Google Analytics