跳转到主要内容

纯Python MySQL驱动程序

项目描述

Documentation Status

Trio-MySQL

此软件包包含一个纯Python和Trio增强的MySQL客户端库。它是PyMySQL的主要直白克隆,增加了与Trio框架兼容的异步方法。

注意:Trio-MySQL试图遵循(异步版本的)在PEP 249中定义的高级数据库API。然而,一些差异是不可避免的。

需求

  • Python – 下面的之一

    • CPython : 3.6 及更高版本

    • PyPy : 最新 3.x 版本

  • MySQL 服务器 – 以下之一

安装

包已上传至 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}

资源

Trio 聊天: https://gitter.im/python-trio/general

许可

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 查看哈希值)

上传时间 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面