PyMySQL 是一个纯 Python 实现的 MySQL 客户端操作库,支持事务、存储过程、批量执行等。PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。
安装
pip install PyMySQL
创建数据库连接
import pymysql connection = pymysql.connect(host='localhost', port=3306, user='root', password='root', db='demo', charset='utf8')
参数列表:
参数 | 描述 |
---|---|
host | 数据库服务器地址,默认 localhost |
user | 用户名,默认为当前程序运行用户 |
password | 登录密码,默认为空字符串 |
database | 默认操作的数据库 |
port | 数据库端口,默认为 3306 |
bind_address | 当客户端有多个网络接口时,指定连接到主机的接口。参数可以是主机名或IP地址。 |
unix_socket | unix 套接字地址,区别于 host 连接 |
read_timeout | 读取数据超时时间,单位秒,默认无限制 |
write_timeout | 写入数据超时时间,单位秒,默认无限制 |
charset | 数据库编码 |
sql_mode | 指定默认的 SQL_MODE |
read_default_file | Specifies my.cnf file to read these parameters from under the [client] section. |
conv | Conversion dictionary to use instead of the default one. This is used to provide custom marshalling and unmarshaling of types. |
use_unicode | Whether or not to default to unicode strings. This option defaults to true for Py3k. |
client_flag | Custom flags to send to MySQL. Find potential values in constants.CLIENT. |
cursorclass | 设置默认的游标类型 |
init_command | 当连接建立完成之后执行的初始化 SQL 语句 |
connect_timeout | 连接超时时间,默认 10,最小 1,最大 31536000 |
ssl | A dict of arguments similar to mysql_ssl_set()'s parameters. For now the capath and cipher arguments are not supported. |
read_default_group | Group to read from in the configuration file. |
compress | Not supported |
named_pipe | Not supported |
autocommit | 是否自动提交,默认不自动提交,参数值为 None 表示以服务器为准 |
local_infile | Boolean to enable the use of LOAD DATA LOCAL command. (default: False) |
max_allowed_packet | 发送给服务器的最大数据量,默认为 16MB |
defer_connect | 是否惰性连接,默认为立即连接 |
auth_plugin_map | A dict of plugin names to a class that processes that plugin. The class will take the Connection object as the argument to the constructor. The class needs an authenticate method taking an authentication packet as an argument. For the dialog plugin, a prompt(echo, prompt) method can be used (if no authenticate method) for returning a string from the user. (experimental) |
server_public_key | SHA256 authenticaiton plugin public key value. (default: None) |
db | 参数 database 的别名 |
passwd | 参数 password 的别名 |
binary_prefix | Add _binary prefix on bytes and bytearray. (default: False) |
执行 SQL
-
cursor.execute(sql, args) 执行单条 SQL
# 获取游标 cursor = connection.cursor() # 创建数据表 effect_row = cursor.execute(''' CREATE TABLE `users` ( `name` varchar(32) NOT NULL, `age` int(10) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ''') # 插入数据(元组或列表) effect_row = cursor.execute('INSERT INTO `users` (`name`, `age`) VALUES (%s, %s)', ('mary', 18)) # 插入数据(字典) info = {'name': 'fake', 'age': 15} effect_row = cursor.execute('INSERT INTO `users` (`name`, `age`) VALUES (%(name)s, %(age)s)', info) connection.commit()
-
executemany(sql, args) 批量执行 SQL
# 获取游标 cursor = connection.cursor() # 批量插入 effect_row = cursor.executemany( 'INSERT INTO `users` (`name`, `age`) VALUES (%s, %s) ON DUPLICATE KEY UPDATE age=VALUES(age)', [ ('hello', 13), ('fake', 28), ]) connection.commit()
注意:INSERT、UPDATE、DELETE 等修改数据的语句需手动执行connection.commit()
完成对数据修改的提交。
获取自增 ID
cursor.lastrowid
查询数据
# 执行查询 SQL cursor.execute('SELECT * FROM `users`') # 获取单条数据 cursor.fetchone() # 获取前N条数据 cursor.fetchmany(3) # 获取所有数据 cursor.fetchall()
游标控制
所有的数据查询操作均基于游标,我们可以通过cursor.scroll(num, mode)
控制游标的位置。
cursor.scroll(1, mode='relative') # 相对当前位置移动 cursor.scroll(2, mode='absolute') # 相对绝对位置移动
设置游标类型
查询时,默认返回的数据类型为元组,可以自定义设置返回类型。支持5种游标类型:
Cursor: 默认,元组类型
DictCursor: 字典类型
DictCursorMixin: 支持自定义的游标类型,需先自定义才可使用
SSCursor: 无缓冲元组类型
SSDictCursor: 无缓冲字典类型
无缓冲游标类型,适用于数据量很大,一次性返回太慢,或者服务端带宽较小时。源码注释:
Unbuffered Cursor, mainly useful for queries that return a lot of data, or for connections to remote servers over a slow network.Instead of copying every row of data into a buffer, this will fetch rows as needed. The upside of this is the client uses much less memory, and rows are returned much faster when traveling over a slow network
or if the result set is very big.There are limitations, though. The MySQL protocol doesn't support returning the total number of rows, so the only way to tell how many rows there are is to iterate over every row returned. Also, it currently isn't possible to scroll backwards, as only the current row is held in memory.
创建连接时,通过 cursorclass 参数指定类型:
connection = pymysql.connect(host='localhost', user='root', password='root', db='demo', charset='utf8', cursorclass=pymysql.cursors.DictCursor)
也可以在创建游标时指定类型:
cursor = connection.cursor(cursor=pymysql.cursors.DictCursor)
事务处理
开启事务
connection.begin()
提交修改
connection.commit()
回滚事务
connection.rollback()
防 SQL 注入
转义特殊字符
connection.escape_string(str)
参数化语句
支持传入参数进行自动转义、格式化 SQL 语句,以避免 SQL 注入等安全问题。
# 插入数据(元组或列表) effect_row = cursor.execute('INSERT INTO `users` (`name`, `age`) VALUES (%s, %s)', ('mary', 18)) # 插入数据(字典) info = {'name': 'fake', 'age': 15} effect_row = cursor.execute('INSERT INTO `users` (`name`, `age`) VALUES (%(name)s, %(age)s)', info) # 批量插入 effect_row = cursor.executemany( 'INSERT INTO `users` (`name`, `age`) VALUES (%s, %s) ON DUPLICATE KEY UPDATE age=VALUES(age)', [ ('hello', 13), ('fake', 28), ])
参考资料
Python中操作mysql的pymysql模块详解
Python之pymysql的使用
相关推荐:
以上是分享一个纯 Python 实现的 MySQL 客户端操作库的详细内容。更多信息请关注PHP中文网其他相关文章!

在数据库优化中,应根据查询需求选择索引策略:1.当查询涉及多个列且条件顺序固定时,使用复合索引;2.当查询涉及多个列但条件顺序不固定时,使用多个单列索引。复合索引适用于优化多列查询,单列索引则适合单列查询。

要优化MySQL慢查询,需使用slowquerylog和performance_schema:1.启用slowquerylog并设置阈值,记录慢查询;2.利用performance_schema分析查询执行细节,找出性能瓶颈并优化。

MySQL和SQL是开发者必备技能。1.MySQL是开源的关系型数据库管理系统,SQL是用于管理和操作数据库的标准语言。2.MySQL通过高效的数据存储和检索功能支持多种存储引擎,SQL通过简单语句完成复杂数据操作。3.使用示例包括基本查询和高级查询,如按条件过滤和排序。4.常见错误包括语法错误和性能问题,可通过检查SQL语句和使用EXPLAIN命令优化。5.性能优化技巧包括使用索引、避免全表扫描、优化JOIN操作和提升代码可读性。

MySQL异步主从复制通过binlog实现数据同步,提升读性能和高可用性。1)主服务器记录变更到binlog;2)从服务器通过I/O线程读取binlog;3)从服务器的SQL线程应用binlog同步数据。

MySQL是一个开源的关系型数据库管理系统。1)创建数据库和表:使用CREATEDATABASE和CREATETABLE命令。2)基本操作:INSERT、UPDATE、DELETE和SELECT。3)高级操作:JOIN、子查询和事务处理。4)调试技巧:检查语法、数据类型和权限。5)优化建议:使用索引、避免SELECT*和使用事务。

MySQL的安装和基本操作包括:1.下载并安装MySQL,设置根用户密码;2.使用SQL命令创建数据库和表,如CREATEDATABASE和CREATETABLE;3.执行CRUD操作,使用INSERT,SELECT,UPDATE,DELETE命令;4.创建索引和存储过程以优化性能和实现复杂逻辑。通过这些步骤,你可以从零开始构建和管理MySQL数据库。

InnoDBBufferPool通过将数据和索引页加载到内存中来提升MySQL数据库的性能。1)数据页加载到BufferPool中,减少磁盘I/O。2)脏页被标记并定期刷新到磁盘。3)LRU算法管理数据页淘汰。4)预读机制提前加载可能需要的数据页。

MySQL适合初学者使用,因为它安装简单、功能强大且易于管理数据。1.安装和配置简单,适用于多种操作系统。2.支持基本操作如创建数据库和表、插入、查询、更新和删除数据。3.提供高级功能如JOIN操作和子查询。4.可以通过索引、查询优化和分表分区来提升性能。5.支持备份、恢复和安全措施,确保数据的安全和一致性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

Atom编辑器mac版下载
最流行的的开源编辑器

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3汉化版
中文版,非常好用