纯Python MySQL驱动程序
项目描述
PyMySQL
此软件包包含一个基于PEP 249的纯Python MySQL客户端库。
要求
安装
软件包已上传至 PyPI。
您可以使用 pip 进行安装
$ python3 -m pip install PyMySQL
要使用 "sha256_password" 或 "caching_sha2_password" 进行身份验证,您需要安装额外的依赖项
$ python3 -m pip install PyMySQL[rsa]
要使用 MariaDB 的 "ed25519" 身份验证方法,您需要安装额外的依赖项
$ python3 -m pip install PyMySQL[ed25519]
文档
文档可在网上找到: https://py.mysqlserver.cn/
有关支持,请参阅 StackOverflow。
示例
以下示例使用了一个简单的表
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) COLLATE utf8_bin NOT NULL,
`password` varchar(255) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
AUTO_INCREMENT=1 ;
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
database='db',
cursorclass=pymysql.cursors.DictCursor)
with connection:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
cursor.execute(sql, ('webmaster@python.org',))
result = cursor.fetchone()
print(result)
此示例将打印
{'password': 'very-secret', 'id': 1}
资源
- DB-API 2.0: https://pythonlang.cn/dev/peps/pep-0249/
- MySQL 参考手册: https://dev.mysqlserver.cn/doc/
- MySQL 客户端/服务器协议: https://dev.mysqlserver.cn/doc/internals/en/client-server-protocol.html
- MySQL Community Slack 中的 "Connector" 频道: https://lefred.be/mysql-community-on-slack/
- PyMySQL 邮件列表: https://groups.google.com/forum/#!forum/pymysql-users
许可
PyMySQL 采用 MIT 许可证发布。有关更多信息,请参阅 LICENSE。
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。
源代码分发
pymysql-1.1.1.tar.gz (47.7 kB 查看哈希值)
构建分发
PyMySQL-1.1.1-py3-none-any.whl (45.0 kB 查看哈希值)