纯 Python MySQL 驱动程序
项目描述
PyMySQL
<nav class="contents local" id="table-of-contents">目录
</nav>这个包包含一个纯 Python MySQL 客户端库,基于PEP 249。
大多数公共 API 都与 mysqlclient 和 MySQLdb 兼容。
注意:PyMySQL 不支持_mysql提供的低级 API,例如data_seek、 store_result和use_result。您应该使用PEP 249中定义的高级 API 。但是支持自动提交和ping等一些 API ,因为PEP 249没有涵盖它们的用例。
要求
安装
包上传到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 ://pymysql.readthedocs.io/
如需支持,请参阅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 ://www.python.org/dev/peps/pep-0249/
MySQL 参考手册:https ://dev.mysql.com/doc/
MySQL 客户端/服务器协议: https ://dev.mysql.com/doc/internals/en/client-server-protocol.html
MySQL Community Slack 中的“连接器”频道: https ://lefred.be/mysql-community-on-slack/
PyMySQL 邮件列表:https ://groups.google.com/forum/#!forum/pymysql-users
执照
PyMySQL 是在 MIT 许可下发布的。有关更多信息,请参阅许可证。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。