pymysql

pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。

1、下载安装

pip3 install pymysql

2、执行SQL

执行SQL语句的基本语法:

需要注意的是:创建连接后,都由游标来进行与数据库的操作,因而获取数据也需要游标。

#!/usr/bin/env python
#-*- coding:utf-8 -*-
__author__ = 'wyf' import pymysql #创建链接
conn = pymysql.connect(host='192.168.14.88',port=3306,user='root',passwd='',db='hive_test')
#创建游标 cursor = conn.cursor()
#print cursor #执行语句,返回数据结果总数量
effect_new = cursor.execute('select * from A')
#print effect_new
#结果是 7
# 执行SQL,并返回受影响行数
effect_row = cursor.executemany('select * from A where aID=%s',[3,4,5])
print effect_row
#结果是3
# 提交,不然无法保存新建或者修改的数据
conn.commit() # 关闭游标
cursor.close()
# 关闭连接
conn.close()

3.获取新建数据的自增ID

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

#!/usr/bin/env python
#-*- coding:utf-8 -*-
__author__ = 'wyf' import pymysql #创建链接
conn = pymysql.connect(host='192.168.14.88',port=3306,user='root',passwd='',db='hive_test')
#创建游标 cursor = conn.cursor()
#print cursor # 执行SQL,并返回受影响行数
effect_row = cursor.executemany('insert into A(aNum) values(%s)',['ggg','hhhh','ffff']) # 提交,不然无法保存新建或者修改的数据
conn.commit()
print cursor.fetchall()
# 关闭游标
cursor.close()
# 关闭连接
conn.close() # 获取最新自增ID,注意是最新的id,多条也是一条
new_id = cursor.lastrowid
print new_id

4、获取查询数据

获取查询数据的三种方式:获取第一行数据,获取前n行数据,获取所有数据 (数据是以元祖的方式存放)

#!/usr/bin/env python
#-*- coding:utf-8 -*-
__author__ = 'wyf' import pymysql #创建链接
conn = pymysql.connect(host='192.168.14.88',port=3306,user='root',passwd='',db='hive_test')
#创建游标 cursor = conn.cursor()
#print cursor #获取所有数据
cursor.execute('select * from A') #获取第一条数据
row_1 = cursor.fetchone()
print 'row_1=',row_1
#获取前N条数据
row_n = cursor.fetchmany(3)
print 'row_n=',row_n
# 获取所有数据
row_all = cursor.fetchall()
print 'row_all=',row_all
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAykAAABaCAIAAAAzVo7PAAAf4ElEQVR4nO3df0wU574/8EnT5MZA2OBJCUKAAIbjNiglitYTtaKS3lNJTe+lGnqT6s2pvaT+PBH1KhgO2iPlVGwLl8tB2mIVjpZ6vrR8QXQBBUTEu/z4SqPSWlF+Kl0KUla6swjP94+ndzqdZ2aZnZ1dwPN+xTTw6cwzn89ndmYfdmdnuXsAAAAA4ClcJwAAAAB4CuZeAAAAAJ7D3QUAAAAAT8HcCwAAAMBzMPcCAAAA8BzMvQAAAAA8hzsBAAAAAJ7CEQAAAADwFMy9AAAAADwHcy94Snz//ejy5bnffz+6cmXedOcC8PTDEQegmQ5zr8zMmpGRnzSs2NDQGRSUuWtXqes5OKY5Q7HS0vZXXvnEHSNnZdWuX/+pi4OoNxtzViM3t+Fvf2uh/9Ww+rVr9/71X09p2/QM7IbAQV0tLT1paReWLMmRxIuKml977TNCyPvvX6Y/eIDKHirl7AGyZwCBpM9CD93Blceqjlw84gD+kTmae01OTh49Wu3vnzF//vuHD5uUFrty5e6XX7Zr2HZs7F9bW3tjYtx+GtWcoVhJSRvHpblj5HffrWJHVuPy5TtGY1ZISGZpqWIOcXEFHJfGcWmRkR/QyPTm7D6ffWYW/ksI+bd/+xstnOPSdu/+asrV4+M/PX/+lrZNz8BuCBzU1dLSk5FRzWael3eVBg8fNnmsLpU9VMrZA2TPAAJJn4UeqnTmTGtISObzzx9varpPI3b7k927vzIYjrz1VonjbU0XyREHAOo5mnt98cX/W7Mmv79/5M6d7zdvPqO02E8/2Xfu1PLalcFwZHx8Qhw5erRawzhT0pyhBHsy1TyypFJtzyWbNp0eGBg1mTqCgzOVlsnNbZiYmLTZ7MI5fXpz9hieH+e4tPHxicrK2//5n+WOF75y5e6qVXmTk5Pqx3dfN7QdBbJrqalLNnMh6Mm9rH5bumSloc9K25Xts1N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X+Pi4cGN9pYhTtH0TkftI8snPzx8fH3dlwKesP2qgh66bsoeudxUAAGbH3Cs9Pb2rq8txRD2nvomoqKjotddec3YTTq3F5tPV1ZWenu7sRsU81p8ZAj10nZoeut5VAACYBXOvtra25ORkpUhjY2NERMSLL754//59lQM69U1EeXl5Gm7N79Rasvns2bNH+MZMZ4n7c+bMmZCQkOeff76pqcmVfFiXL182Go0hISHCF1AK32XZ39+vFFmzZg39toPAwEClZYaGhiorK9evXy9si41I6nJrDwkhIyMjERER6lefFT2U1KWyh650FQAAyKyYe+3YsaOurk4psnLlyqqqqosXL7799ttuSkDb1yK5+GVKtbW1O3fu1LauuD8xMTE//vhjaWmp7i/DbNq0aWBgwGQyBQcH08ihQ4eys7MLCgoOHjyoFCkpKZGMwy5jNpvXrVsnbiAbUVOXXj2kv7rj27Gmt4dq6mJ76EpXAQCATNfcy6nvfDQajYODg0oRLy+vsbExm80WHh7uvmw9tpbAYrEYjUZt64r7c+/ePULI2NhYQECAK/mwUlNTCSE2my0sLIxGoqOje3p6+vr6XnjhBaUIO29gl6HYBoojaurSq4cmkykxMdEdc6/p7aGautgeutJVAAAgjude9A9lf39/k8m0fPlyGmxvb4+Kilq0aFF7ezshhOO4LVu2xMTEbNiwYf/+/bLn8VdffXXBggWVlZV+fn4aXp3y8vKy2+1KkYiIiKamppqaGi8vL2dHFjt27Jifn19gYGBCQgKNXLt2LTw8fOvWrUJRmZmZQUFBPj4+27dv13ctFs/z3t7e2mqRdGx0dDQnJ6egoEDbaBRbBVVTUyNckO7r68vzPM/zvr6+SpGMjIylS5eGh4fX19crLUM5njeoqUuXHg4NDf3hD38YGxtzfe41o3qosi62h650FQAAiOO519DQUH19PcdxHR0dHR0dNBgXF2cymaqqquhHooqLi8vLyy9cuJCRkWE2my9dusSO09DQwHFcY2NjZ2fns88+S5x83WvBggWS173EkcLCQj8/v6ysLBdf9woKCmppaeF5/u7duzSyYsWK8+fPt7a2CukVFhZaLJbm5mYhotdarMHBQc2vLkg6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z/D4Hu1rDwVnSLAAAAAElFTkSuQmCC" alt="" />

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

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

示例:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
__author__ = 'wyf' import pymysql #创建链接
conn = pymysql.connect(host='192.168.14.88',port=3306,user='root',passwd='',db='hive_test')
#创建游标 cursor = conn.cursor()
#print cursor #获取所有数据
cursor.execute('select * from A')
#所有查询数据都打印一编,方便对比
print '''row_all= ((1, 'a20050111'), (9, 'sdasdas'), (3, 'a20050113'), (4, 'a20050114'), (5, 'a20050115'), (6, 'a20160928'), (7, 'sdasdasdadasd'), (8, 'sdasdasdadasd'), (17, 'ffff'), (16, 'hhhh'), (15, 'ggg'))'''
cursor.scroll(1, mode='relative') # 相对当前位置移动
row_all_1 = cursor.fetchone()
print 'row_all_1=',row_all_1
cursor.scroll(2, mode='absolute') # 相对绝对位置移动
row_all_2 = cursor.fetchone()
print 'row_all_2=',row_all_2
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABOUAAABECAIAAABBMhaIAAAZoklEQVR4nO3df0xUZ7oH8DebTTYNhAlullAIEMSwTgNSAmhtxIrKH1vJmm5QQzdZTXbtElFpI2IEDJfaOOuushu57uzUWlyVq/WSTJeA6AAWEBHviBO4Uen2WuVn4UJRyjg7ZxDf+8fpnsyed2Y4c86ZmQP3+4l/DI/nfc/7PO85Z+blMDNk3A2Vpqrq+s6dl7791v7o0WR1dbvvjXfv/k+bbYR/3NX1tcv14uHD8cTE3/ORnJy/dHT8T3v7Vzk5f+EjHPeCkEqOe+HeyQ9/WDky8rSt7e/8j9u3X0hL+1N5+dXDh5v4SEzM7x48GB8bmyGk8s6dJ5RSl0vcT3a28datr51OF7+BxxGyPbMjFPXsdLr4H//2t/9ub//KW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xPTx8ZGRkZGRHeU8pGhoeHKaVOpzMiIsLbNjzfFZOSV2dnZ1FRkdyMAQAAAAAAVCZzvepyuQghHMcJkZycnI6Ojvb2dmFFtH379rS0tPLy8sOHD3vrJyYm5sGDB2NjY4SQO3fu8EGn00kIcTgcwmZsROT+/fuU0vn5+ZqaGulZ8KxWa2lpKf84IyNjdnbWarUmJibyEXnfrcrmxY6woqLi9OnTH3/8Mf/WVo+Rt956y263NzU1rV+/3ts2lNLx8XFCCP8mVY8RNi/WwYMHrVarjGQBAAAAAAACQc561Ww2C39nK7wbs7e3NyUlJTU1VfhOlAsXLjQ1NbW2tnZ3d3vrqri4WKfTmUym/Pz8vLw8Sml1dbXQeXV1tceIhzT+acOGDdKTF5SVlQ0NDVFKz507Fx0dnZKS0traShV8tyqbFztC4ZtpvvnmG2+Rurq62NjYlJQUoYbsNhaLReic/7IcNiLKizU0NFReXi4jUwAAAAAAgADR6OcDBxnHcUajkY1L/G7VJcBoNLrfLQcAAAAAAAi54K1X/foMpCD0AwAAAAAAAFqG+6sAAAAAAACgReTw4cNYrwIAAAAAAIDWkMuXL2O9CgAAAAAAAFpDvvrqK6xXAQAAAAAAQGvIuJtQDwYAAAAAAADge1ivAgAAAAAAgBZhvQoAAAAAAABa9H8+1zeleTsakQAAAABJRU5ErkJggg==" alt="" />

总结:

1.当用fetchall的时候游标指针会移动到最后,请在使用scroll的时候注意。

2.结果分析,相对是当前指针的位置,绝对是指针整体位置。

3.(1, mode='relative') 当前指针下移一个的位置,及地第二个,(2, mode='absolute'),最初的指针下移两个,因为这个结果指针最初都是最开始。

5.fetch数据类型

默认拿到的数据是元祖类型,如果是字典的话会更方便操作,那方法来了:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
__author__ = 'wyf' import pymysql #创建链接
conn = pymysql.connect(host='192.168.14.88',port=3306,user='root',passwd='',db='hive_test')
#创建游标 cursor = conn.cursor()
#print cursor # 游标设置为字典类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
r = cursor.execute("select * from A")
result = cursor.fetchone()
print 'result=',result
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAj4AAAApCAIAAAC6HLTxAAAQr0lEQVR4nO2df0xUV/bAbzakG5OGydBIUIJGJI3TsLVE0WiKVZRoK1mzu+wS12R1E7uSasR07f7o6k7rWAYK4g8WEZH6C4o/6CKsih2hKA6IDoNlqDCKjAoDFalQywjzhh/3+8fF5+u7d4bHYxhm/J5PTsibw73nnnPe/THcd4dBemMLCAgICAiIDwm6VnsPBAQEBATEhwSWLhAQEBAQHxNUaWgGAQEBAQHxIYGlCwQEBATExwSWLhAQEBAQHxNYukBAQEBAfEzQvzXpICAgICAgPiQIAwAAAIBPAUsXAAAA4GPA0gX4No8f9y5alPn4cW9UVNZk+wIALz9eMuLkL10pKeVPn/bLqKjXW0JCUhITi2Q3LRHZHgopKjK9917uRFjes+fK6tVfjNOIdHzRZylkZuq//NJIfsqofv36/d/97oS8pr0wGzwu4jIa29TqS/PnZ4j0eXm1v/nNcYxxamoFufAAEnPozGcPwJwBeER55nM4EYynr7qRcY44d8FYuoaHh5OSyoKCtGFhqbt26ZzVvHat5dw5k4wmly07VFdnjYyc8F4o20MhZ87cQkg9EZZ3775MW5ZCRUWzSrVn5syUoiKnPsTE5CCkRkgdHr6XaCbX54nj+HED/xNjvG7dlyRwhNTbthWPWj029ouLFxvlNe2F2eBxEZfR2KbVltGeZ2VVEeWuXTqPxSUxh8589gDMGYBHlGc+hxIpKKibOTPljTfSa2oeEI3DMbhtW7FCodm48YzrtiYL0YibLBhL19mz30ZHZ3d0PG1ufrx+fYGzmv39jq1b5fzlpFBoBgaGhJqkpDIZdkZFtoci6L4o27IoUnlDMT7+ZGdnr05nnjEjxVmZzEz90NCw3e7gh8Tk+uwxOG4AIfXAwFBpadM//nHedeFr11qWLMkaHh6Wbn/isiFvFDBrSYmL6Tmv9ORdlt6WW7ySkWdn7TLzPCY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ic5P+kW6Kzevn0bYzw0NMTncMeOHZmZmYcPHyaPwZiad955x2azXbhwgd/PpMtgKqsSNSI88wwSAACPwRjSRUVF/PYd//TCaDRGRkYGBQUVFBQQTWxsbGhoqFKp1Gg0RLNx48bXXnvt9OnToaGh5Bih6DA6KbZp06aAgIAjR47IcFepVN68eZPZFk1iYqJCocjOzo6Li4uNjaX9IRdz5sxJTk4eGBhw0e6oxxl0Oh1vuaioSOInomREwZ+JF+4EiuwcPHgQIZSQkMD/PHjwoLy4RvUHC3Z9ly5dSsrwx9y///57Z5r8/Pzg4ODw8PDq6mpnZURZlagR8e2338LnugDgJcPH3o2mpqbyO5bejOzPhwFuZ+rUqeP85AMAAN6Gjy1dAAAAAPB/BK6jp7GzlSoAAAAASUVORK5CYII=" alt="" />

6.利用with自动关闭

# 利用with定义函数

    @contextlib.contextmanager
def mysql(self, host='192.168.14.88',port=3306,user='root',passwd='',db='hive_test', charset='utf8'):
self.conn = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db, charset=charset)
self.cuersor = self.conn.cursor(cursor=pymysql.cursors.DictCursor) try:
yield self.cuersor
finally:
self.conn.commit()
self.cuersor.close()
self.conn.close() # 执行
with mysql() as cuersor:
print(cuersor)
# 操作MySQL代码块

Python 操作 MySQL--(pymysql)的更多相关文章

  1. mysql数据库----python操作mysql ------pymysql和SQLAchemy

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy 一.pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQ ...

  2. day40:python操作mysql:pymysql模块&SQL注入攻击

    目录 part1:用python连接mysql 1.用python连接mysql的基本语法 2.用python 创建&删除表 3.用python操作事务处理 part2:sql注入攻击 1.s ...

  3. python成长之路【第十三篇】:Python操作MySQL之pymysql

    对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎 ...

  4. Python操作MySQL:pymysql和SQLAlchemy

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  5. Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy

    本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...

  6. Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  7. Python操作Mysql之基本操作

    pymysql python操作mysql依赖pymysql这个模块 下载安装 pip3 install pymysql 操作mysql python操作mysql的时候,是通过”游标”来进行操作的. ...

  8. Python 操作 MySQL 之 pysql 与 ORM(转载)

    本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...

  9. Python开发【第十九篇】:Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  10. Python全栈开发之MySQL(二)------navicate和python操作MySQL

    一:Navicate的安装 1.什么是navicate? Navicat是一套快速.可靠并价格相宜的数据库管理工具,专为简化数据库的管理及降低系统管理成本而设.它的设计符合数据库管理员.开发人员及中小 ...

随机推荐

  1. C语言的本质(13)——指向指针的指针

    指针可以指向基本类型,也可以指向复合类型,因此一个指针变量存放的可以是另一个指针变量的地址,则称这个指针变量为指向指针的指针变量.由于指针变量直接指向变量,所以称为"单级间址".而 ...

  2. curl几个选项

    1.--cacert  选项请看https://curl.haxx.se/docs/sslcerts.html 2.CURL库怎样验证服务器证书 [复制链接] 中提到:你是客户端, 你希望的是: 你拿 ...

  3. hdu 4712 Hamming Distance ( 随机算法混过了 )

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  4. chrome调试工具常用功能整理(转)

    Elements chrome devtools 中 Elements panel 是审查 dom 元素和 css 的, 可以实时修改 dom/css. windows: ctrl + shift + ...

  5. 关于ASP.NET中的负载均衡

    ASP.NET站点中做负载均衡: 基于HTTP协议我们可能发现我们要解决两点问题: 第一做到负载均衡,我们需要一个负载均衡器. 可以通过DNS轮询来做,在DNS服务器上配置为每次对我们做负载均衡的同一 ...

  6. java中驼峰与下横线格式字符串互转算法

    public static final char UNDERLINE = '_'; /** * 驼峰格式字符串转换为下划线格式字符串 * * @param param * @return */ pub ...

  7. strutr2运行流程

    1. 请求发送给 StrutsPrepareAndExecuteFilter 2. StrutsPrepareAndExecuteFilter 询问 ActionMapper: 该请求是否是一个 St ...

  8. form表单中经常用到的禁用获取值问题

    <input name="country" id="country" size=12 value="disabled提交时得不到该值 " ...

  9. 关闭编译器FPO优化

    // The release libs don't include FPO debug information, so FPO// optimization will interfere with s ...

  10. windows系统——mysql自动定时备份数据库的最佳方法

    网上有很多关于window下Mysql自动备份的方法,可是真的能用的也没有几个,有些说的还非常的复杂,难以操作. 我们都知道mssql本身就自带了计划任务可以用来自动备份,可是mysql咱们要怎么样自 ...