使用PyMySQL操作mysql数据库
2016-05-03 22:45
579 查看
适用环境
python版本 >=2.6或3.3mysql版本>=4.1
安装
可以使用pip安装也可以手动下载安装。使用pip安装,在命令行执行如下命令:
1 | pip install PyMySQL |
其中的X.X是版本(目前可以获取的最新版本是0.6.6)。
下载后解压压缩包。在命令行中进入解压后的目录,执行如下的指令:
1 | python setup.py install |
使用示例
连接数据库如下:1 2 3 4 5 6 7 8 9 10 | import pymysql.cursors # Connect to the database connection = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='zhyea.com', db='employees', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import pymysql.cursors config = { 'host':'127.0.0.1', 'port':3306, 'user':'root', 'password':'zhyea.com', 'db':'employees', 'charset':'utf8mb4', 'cursorclass':pymysql.cursors.DictCursor, } # Connect to the database connection = pymysql.connect(**config) |
执行sql语句前需要获取cursor,因为配置默认自动提交,故在执行sql语句后需要主动commit,最后不要忘记关闭连接:
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 27 28 29 30 | from datetime import date, datetime, timedelta import pymysql.cursors #连接配置信息 config = { 'host':'127.0.0.1', 'port':3306, 'user':'root', 'password':'zhyea.com', 'db':'employees', 'charset':'utf8mb4', 'cursorclass':pymysql.cursors.DictCursor, } # 创建连接 connection = pymysql.connect(**config) # 获取明天的时间 tomorrow = datetime.now().date() + timedelta(days=1) # 执行sql语句 try: with connection.cursor() as cursor: # 执行sql语句,插入记录 sql = 'INSERT INTO employees (first_name, last_name, hire_date, gender, birth_date) VALUES (%s, %s, %s, %s, %s)' cursor.execute(sql, ('Robin', 'Zhyea', tomorrow, 'M', date(1989, 6, 14))); # 没有设置默认自动提交,需要主动提交,以保存所执行的语句 connection.commit() finally: connection.close(); |
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 27 28 29 30 31 32 33 34 | import datetime import pymysql.cursors #连接配置信息 config = { 'host':'127.0.0.1', 'port':3306, 'user':'root', 'password':'zhyea.com', 'db':'employees', 'charset':'utf8mb4', 'cursorclass':pymysql.cursors.DictCursor, } # 创建连接 connection = pymysql.connect(**config) # 获取雇佣日期 hire_start = datetime.date(1999, 1, 1) hire_end = datetime.date(2016, 12, 31) # 执行sql语句 try: with connection.cursor() as cursor: # 执行sql语句,进行查询 sql = 'SELECT first_name, last_name, hire_date FROM employees WHERE hire_date BETWEEN %s AND %s' cursor.execute(sql, (hire_start, hire_end)) # 获取查询结果 result = cursor.fetchone() print(result) # 没有设置默认自动提交,需要主动提交,以保存所执行的语句 connection.commit() finally: connection.close(); |
![](http://7xnmwg.com1.z0.glb.clouddn.com/qiniu/224/image/02ef9714b1ed925029b464256a165b01.png?imageView2/2/w/782/h/76)
从结果集中获取指定数目的记录,可以使用fetchmany方法:
1 | result = cursor.fetchmany(2) |
获取全部结果集可以使用fetchall方法:
1 | result = cursor.fetchall() |
1 | [{'last_name': 'Vanderkelen', 'hire_date': datetime.date(2015, 8, 12), 'first_name': 'Geert'}, {'last_name': 'Zhyea', 'hire_date': datetime.date(2015, 8, 21), 'first_name': 'Robin'}] |
在django中使用
在django中使用是我找这个的最初目的。目前同时支持python3.4、django1.8的数据库backend并不好找。这个是我目前找到的最好用的。设置DATABASES和官方推荐使用的MySQLdb的设置没什么区别:
1 2 3 4 5 6 7 8 9 10 | DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mytest', 'USER': 'root', 'PASSWORD': 'zhyea.com', 'HOST': '127.0.0.1', 'PORT': '3306', } } |
1 2 | import pymysql pymysql.install_as_MySQLdb() |
相关文章推荐
- mysql
- mysql中 REPLACE INTO 和 INSERT INTO 的区别
- mysql中 REPLACE INTO 和 INSERT INTO 的区别
- 基于mysql5.6之GTID新特性的主从模式下的amoeba代理
- MySQL性能指标计算方式
- 【MySQL】CSV 文件导入MySQL
- SQL语句LIMIT在MySQL中的性能分析
- Ubuntu 12.04.1 mysql从5.5升级到5.6
- MYSQL学习笔记 (一)
- 对MySQL数据库数据操作之修改
- 利用Navicat for Mysql创建数据库
- 关于mysql数据库不是3306端口用命令行远程连接的解决办法
- 利用jemalloc优化mysql
- MySQL 子查询
- mysql复制
- 跟我一起学习MySQL技术内幕(第五版):(第三章学习日记1)
- 第96课: 通过Spark Streaming的foreachRDD把处理后的数据写入外部存储系统中
- MySql模糊查询like通配符使用详细介绍
- maxscale实现读写分离
- Mysql 主从同步(转载)