纯Python MySQL驱动程序
项目描述
Trio-MySQL
此软件包包含一个纯Python和Trio增强的MySQL客户端库。它是PyMySQL的主要直白克隆,增加了与Trio框架兼容的异步方法。
注意:Trio-MySQL试图遵循(异步版本的)在PEP 249中定义的高级数据库API。然而,一些差异是不可避免的。
需求
安装
包已上传至 PyPI。
您可以使用 pip 安装它
$ python3 -m pip install trio_mysql
要使用“sha256_password”或“caching_sha2_password”进行身份验证,您需要安装额外的依赖项
$ python3 -m pip install trio_mysql[rsa]
要使用 MariaDB 的“ed25519”身份验证方法,您需要安装额外的依赖项
$ python3 -m pip install PyMySQL[ed25519]
文档
文档可在网上找到: http://trio_mysql.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 trio_mysql.cursors
# Connect to the database
connection = trio_mysql.connect(host='localhost',
user='user',
password='passwd',
charset='utf8mb4',
database='db',
cursorclass=trio_mysql.cursors.DictCursor)
async with connection as conn:
async with conn.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
await cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
# connection is not autocommit by default. So you must commit to save
# your changes.
conn.commit()
# You can set up a transaction:
async with conn.transaction():
async with conn.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
await cursor.execute(sql, ('webmistress@python.org', 'totally-secret'))
# ... or use a cursor directly, for autocommit:
async with conn.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
await cursor.execute(sql, ('webmaster@python.org',))
result = await 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/
许可
Trio-MySQL 在 MIT 许可证下发布。有关更多信息,请参阅 LICENSE。
项目详情
下载文件
下载适合您平台的文件。如果您不确定要选择哪个,请了解有关 安装包 的更多信息。
源分发
trio_mysql-1.0.3.tar.gz (47.4 kB 查看哈希值)
构建分发
trio_mysql-1.0.3-py2.py3-none-any.whl (45.3 kB 查看哈希值)
关闭
trio_mysql-1.0.3.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 110de7b9ddc77cd9cfeaa08492d2b0dceafd393bc5ab2daec2be30114fea83e3 |
|
MD5 | 643d3b6fc8010a86425cbf0e235838e3 |
|
BLAKE2b-256 | ebeb3f5a8db4b9475c7f8b3ae0925689ec556065443374b6ac4c6e2c9ad71395 |
关闭
trio_mysql-1.0.3-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1a59acd39f439219ee87e0c23f507cecc18f7b282fca1f4253c34cef44d942ab |
|
MD5 | 29232ebe0935a122ffb9f3a22bbb7dbc |
|
BLAKE2b-256 | c179c62d91546a81b8fab18278d57aac8533e415eecd30078f2b5da79a4e63bd |