跳转到主要内容

纯Python MySQL驱动程序

项目描述

Documentation Status codecov

PyMySQL

此软件包包含一个基于PEP 249的纯Python MySQL客户端库。

要求

  • Python -- 以下之一
    • CPython : 3.7及更高版本
    • 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://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}

资源

许可

PyMySQL 采用 MIT 许可证发布。有关更多信息,请参阅 LICENSE。

项目详情


下载文件

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

源代码分发

pymysql-1.1.1.tar.gz (47.7 kB 查看哈希值)

上传时间 源代码

构建分发

PyMySQL-1.1.1-py3-none-any.whl (45.0 kB 查看哈希值)

上传时间 Python 3

由以下组织支持