Giter VIP home page Giter VIP logo

trio-mysql's Introduction

Documentation Status https://travis-ci.org/python-trio/trio-mysql.svg?branch=master

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.

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

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

$ pip install trio_mysql

Documentation is available online: http://trio_mysql.readthedocs.io/

For support, please refer to the StackOverflow.

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, ('[email protected]', '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, ('[email protected]', '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, ('[email protected]',))
        result = await cursor.fetchone()
        print(result)

This example will print:

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

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

Trio chat: https://gitter.im/python-trio/general

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

trio-mysql's People

Contributors

bbigras avatar boglin avatar clarkevans avatar clelland avatar cleure avatar cyphergrue avatar djmitche avatar elemount avatar grooverdan avatar hannosch avatar jamadden avatar jcotton1123 avatar jvankoten avatar klondi avatar lecram avatar martinthoma avatar methane avatar mgrechukh avatar mlongtin0 avatar petehunt avatar raggiskula avatar rfk avatar rjollos avatar smurfix avatar tonal avatar wiggzz avatar worldexception avatar wraziens avatar yoriksar avatar zzzeek avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.