Skip to main content

ODBC driver for asyncio.

Project description

https://travis-ci.org/aio-libs/aioodbc.svg?branch=master https://coveralls.io/repos/aio-libs/aioodbc/badge.svg?branch=master&service=github

aioodbc is Python 3.5+ module that makes possible accessing ODBC databases with asyncio. It is rely on awesome pyodbc library, preserve same look and feel. aioodbc was written async/await syntax (PEP492) thus not compatible with Python older then 3.5. Internally aioodbc employ threads to avoid blocking the event loop, btw threads are not that bad as you think, other drivers like motor use same approach.

aioodbc fully compatible and tested with uvloop. Take a look on a test suite, all tests are executed with both: default and uvloop.

Supported Databases

aioodbc should work with all databases supported by pyodbc. But for now library have been tested with: SQLite, MySQL and PostgreSQL. Feel free to add other databases to the test suite by submitting PR.

Community

Mailing List: https://groups.google.com/forum/#!forum/aio-libs

Chat room: https://gitter.im/aio-libs/Lobby

Basic Example

aioodbc based on pyodbc , and provides same api, you just need to use yield from conn.f() or await conn.f() instead of just call conn.f() for every method.

Properties are unchanged, so conn.prop is correct as well as conn.prop = val.

import asyncio
import aioodbc


loop = asyncio.get_event_loop()


async def test_example():
    dsn = 'Driver=SQLite;Database=sqlite.db'
    conn = await aioodbc.connect(dsn=dsn, loop=loop)

    cur = await conn.cursor()
    await cur.execute("SELECT 42;")
    r = await cur.fetchall()
    print(r)
    await cur.close()
    await conn.close()

loop.run_until_complete(test_example())

Connection Pool

Connection pooling ported from aiopg and rely on PEP492 features:

import asyncio
import aioodbc


loop = asyncio.get_event_loop()


async def test_pool():
    dsn = 'Driver=SQLite;Database=sqlite.db'
    pool = await aioodbc.create_pool(dsn=dsn, loop=loop)

    async with pool.acquire() as conn:
        cur = await conn.cursor()
        await cur.execute("SELECT 42;")
        r = await cur.fetchall()
        print(r)
        await cur.close()
        await conn.close()
    pool.close()
    await pool.wait_closed()

loop.run_until_complete(test_example())

Context Managers

Pool, Connection and Cursor objects support context manager protocol:

import asyncio
import aioodbc


loop = asyncio.get_event_loop()


async def test_example():
    dsn = 'Driver=SQLite;Database=sqlite.db'

    async with aioodbc.create_pool(dsn=dsn, loop=loop) as pool:
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute('SELECT 42;')
                val = await cur.fetchone()
                print(val)

loop.run_until_complete(test_example())

Installation

In Linux environment pyodbc (hence aioodbc) requires unixODBC library. You can install it using package manager from your OS distribution, for example:

$ sudo apt-get install unixodbc
$ sudo apt-get install unixodbc-dev

then:

pip install aioodbc

Run tests

For testing purposes you need to install docker and development requirements:

$ pip install -r requirements-dev.txt

In order to simplify development all tests and environment created inside separate docker image, you do not need to install any database of system level libraries, everything happens automatically inside container.

Then just execute:

$ make docker_build
$ make docker_test

Test will automatically pull images and build containers with required databases.

Other SQL Drivers

  • aiopg - asyncio client for PostgreSQL

  • aiomysql - asyncio client form MySQL

Requirements

Changes

0.0.4 (2017-04-30)

  • Improved mysql testing

0.0.3 (2016-07-05)

  • Dockerize tests, now we can add more DBs to tests using docker #15, #17, #19

  • Test suite executed with both default asyncio and uvloop #18

0.0.2 (2016-01-01)

  • Improved pep 492 support.

  • pool.get method removed, use acquire instead.

  • Added tests against MySQL.

  • Added bunch of doc strings.

0.0.1 (2015-10-12)

  • Initial release.

Project details


Download files

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

Source Distribution

aioodbc-0.1.0a0.tar.gz (17.3 kB view details)

Uploaded Source

File details

Details for the file aioodbc-0.1.0a0.tar.gz.

File metadata

  • Download URL: aioodbc-0.1.0a0.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aioodbc-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 88eba5012acbaeca4d8618f22c2ce5c7d412b9d254777ef5ee29007fdaed3546
MD5 7582bcb70ed238a044bb64d81c514eae
BLAKE2b-256 1e7d33d829151bc2fe124a422ce0eda861adfb9733728edb5308606f5cc36642

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page