Skip to main content

纯 Python MySQL 驱动程序

项目描述

文件状态 https://coveralls.io/repos/PyMySQL/PyMySQL/badge.svg?branch=master&service=github https://img.shields.io/lgtm/grade/python/g/PyMySQL/PyMySQL.svg?logo=lgtm&logoWidth=18

PyMySQL

<nav class="contents local" id="table-of-contents">

目录

</nav>

这个包包含一个纯 Python MySQL 客户端库,基于PEP 249

大多数公共 API 都与 mysqlclient 和 MySQLdb 兼容。

注意:PyMySQL 不支持_mysql提供的低级 API,例如data_seekstore_resultuse_result您应该使用PEP 249中定义的高级 API 。但是支持自动提交ping等一些 API ,因为PEP 249没有涵盖它们的用例。

要求

  • Python – 以下之一:

    • CPython:3.6 及更新版本

    • PyPy:最新的 3.x 版本

  • 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 ://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}

资源

执照

PyMySQL 是在 MIT 许可下发布的。有关更多信息,请参阅许可证。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

PyMySQL-1.0.2.tar.gz (45.5 kB 查看哈希

已上传 source

内置分布

PyMySQL-1.0.2-py3-none-any.whl (43.8 kB 查看哈希

已上传 py3