PyMySQL

Pure Python MySQL Driver

46 个版本 Python >=3.9
安装
pip install PyMySQL
poetry add PyMySQL
pipenv install PyMySQL
conda install PyMySQL
描述

Documentation Status codecov Ask DeepWiki

PyMySQL

This package contains a pure-Python MySQL and MariaDB client library, based on PEP 249.

Requirements

  • Python -- one of the following:
  • MySQL Server -- one of the following:

Installation

Package is uploaded on PyPI.

You can install it with pip:

$ python3 -m pip install PyMySQL

To use "sha256_password" or "caching_sha2_password" for authenticate, you need to install additional dependency:

$ python3 -m pip install PyMySQL[rsa]

To use MariaDB's "ed25519" authentication method, you need to install additional dependency:

$ python3 -m pip install PyMySQL[ed25519]

Documentation

Documentation is available online: https://pymysql.readthedocs.io/

For support, please refer to the StackOverflow.

Example

The following examples make use of a simple table

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)

This example will print:

{'password': 'very-secret', 'id': 1}

Resources

License

PyMySQL is released under the MIT License. See LICENSE for more information.

版本列表
1.2.0 2026-05-19
1.1.3 2026-05-01
1.1.2 2025-08-24
1.1.1 2024-05-21
1.1.0 2023-06-26
1.1.0rc2 2023-06-15
1.1.0rc1 2023-05-25
1.0.3 2023-03-28
1.0.2 2021-01-09
1.0.1 2021-01-08
1.0.0 2021-01-07
1.0.3rc1 2023-03-24
0.10.1 2020-09-10
0.10.0 2020-07-18
0.9.3 2018-12-18
0.9.2 2018-07-04
0.9.1 2018-07-02
0.9.0 2018-06-27
0.8.1 2018-05-07
0.8.0 2017-12-20
0.7.11 2017-04-05
0.7.10 2017-02-14
0.7.9 2016-09-03
0.7.8 2016-09-01
0.7.7 2016-08-29
0.7.6 2016-07-29
0.7.5 2016-06-28
0.7.4 2016-05-26
0.7.3 2016-05-19
0.7.2 2016-02-24
0.7.1 2016-01-14
0.7.0 2016-01-10
0.6.7 2015-09-30
0.6.6 2015-02-28
0.6.4 2015-02-25
0.6.4.dev1 2015-02-25
0.6.3 2014-12-02
0.6.2 2014-04-21
0.6.1 2013-11-21
0.6 2013-10-04
0.5 2011-11-08
0.4 2010-12-27
0.3 2010-12-14
0.2 2009-06-16