RETIRED, further work has moved to Debian project infrastructure
Go to file
INADA Naoki 52980174ce Merge pull request #585 from elemount/authissue
Fix handle AuthSwitch packet bug.
2017-06-29 18:54:47 +09:00
.github Small grammar chenge to ISSUE_TEMPLATE.md (#544) 2017-01-27 15:12:11 +09:00
.travis Reduce top level files for travis 2016-05-17 20:40:03 +09:00
docs Update API docs (#573) 2017-05-25 01:06:50 +09:00
pymysql Fix handle AuthSwitch packet bug. 2017-06-29 11:20:50 +08:00
.coveragerc merge from master 2015-08-31 08:28:20 +10:00
.gitignore Add Sphinx documentation 2016-05-17 12:45:30 +02:00
.travis.yml fix travis build (#543) 2017-01-13 17:55:39 +09:00
CHANGELOG 0.7.11 2017-04-06 03:07:07 +09:00
LICENSE Change brief description and update year in copyright notice. 2013-10-03 09:46:31 -03:00
MANIFEST.in Add test files and example to distribution. 2013-10-04 14:18:17 -03:00
README.rst Fix PEP reference in README (#514) 2016-09-05 18:58:01 +09:00
example.py Fix PEP8 in example 2016-01-15 11:09:13 +09:00
runtests.py Fix atexit error on Travis 2016-05-10 01:00:44 +09:00
setup.cfg remove deprecated config 2016-07-29 12:27:36 +09:00
setup.py Fix `TypeError` with Python 2.7 (#575) 2017-05-29 14:38:52 +09:00
tox.ini add PAM plugin to test suite 2015-08-29 23:38:56 +10:00

README.rst

Documentation Status

image

image

image

PyMySQL

Table of Contents

This package contains a pure-Python MySQL client library. The goal of PyMySQL is to be a drop-in replacement for MySQLdb and work on CPython, PyPy and IronPython.

NOTE: PyMySQL doesn't support low level APIs _mysql provides like data_seek, store_result, and use_result. You should use high level APIs defined in PEP 249. But some APIs like autocommit and ping are supported because PEP 249 doesn't cover their usecase.

Requirements

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

Installation

The last stable release is available on PyPI and can be installed with pip:

$ pip install PyMySQL

Documentation

Documentation is available online: http://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=utf8 COLLATE=utf8_bin
AUTO_INCREMENT=1 ;
import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             db='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

try:
    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)
finally:
    connection.close()

This example will print:

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

Resources

DB-API 2.0: http://www.python.org/dev/peps/pep-0249

MySQL Reference Manuals: http://dev.mysql.com/doc/

MySQL client/server protocol: http://dev.mysql.com/doc/internals/en/client-server-protocol.html

PyMySQL mailing list: https://groups.google.com/forum/#!forum/pymysql-users

License

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