Skip to main content

Pure Python MySQL Driver

Project description

Documentation Status https://travis-ci.org/python-trio/trio-mysql.svg?branch=master https://codecov.io/gh/python-trio/trio-mysql/branch/master/graph/badge.svg https://img.shields.io/badge/license-MIT-blue.svg

Trio-MySQL

This package contains a pure-Python and Trio-enhanced MySQL client library. It is a mostly-straightforward clone of PyMySQL, adding async methods compatible with the Trio framework.

NOTE: Trio-MySQL tries to adhere to (an async version of) the high level database APIs defined in PEP 249. Some differences, however, are unavoidable.

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 trio_mysql

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

$ python3 -m pip install trio_mysql[rsa]

Documentation

Documentation is available online: http://trio_mysql.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 trio_mysql.cursors

# Connect to the database
connection = trio_mysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             db='db',
                             charset='utf8mb4',
                             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()

    # Alternately, 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'))

    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)

This example will print:

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

License

Trio-MySQL is released under the MIT License. See LICENSE for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for trio_mysql, version 0.9.3
Filename, size File type Python version Upload date Hashes
Filename, size trio_mysql-0.9.3.tar.gz (91.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page