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 https://img.shields.io/pypi/v/aioodbc.svg Chat on Gitter

aioodbc is a Python 3.5+ module that makes it possible to access ODBC databases with asyncio. It relies on the awesome pyodbc library and preserves the same look and feel. aioodbc was written using async/await syntax (PEP492) and thus is not compatible with Python versions older than 3.5. Internally aioodbc employs threads to avoid blocking the event loop, threads are not that as bad as you think!. Other drivers like motor use the same approach.

aioodbc is fully compatible and tested with uvloop. Take a look at the test suite, all tests are executed with both the default event loop and uvloop.

Supported Databases

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

Community

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

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

Basic Example

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

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 AS age;")
    rows = await cur.fetchall()
    print(rows)
    print(rows[0])
    print(rows[0].age)
    await cur.close()
    await conn.close()

loop.run_until_complete(test_example())

Connection Pool

Connection pooling is ported from aiopg and relies 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_pool())

Context Managers

Pool, Connection and Cursor objects support the context management 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 AS age;')
                val = await cur.fetchone()
                print(val)
                print(val.age)

loop.run_until_complete(test_example())

Installation

In a linux environment pyodbc (hence aioodbc) requires the unixODBC library. You can install it using your package manager, 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 the development requirements:

$ pip install -r requirements-dev.txt

In order to simplify development you should install the provided docker container. This way you don’t need to install any databases or other system libraries, everything happens inside the container.

Then just execute:

$ make docker_build
$ make docker_test

The test will automatically pull images and build containers with the required databases.

NOTE: Running tests requires Python 3.6 or higher.

Other SQL Drivers

  • aiopg - asyncio client for PostgreSQL

  • aiomysql - asyncio client form MySQL

Requirements

Changes

0.3.2 (2018-08-04)

  • Added basic documentation for after_created and ThreadPoolExecutor #176 (thanks @AlexHagerman)

  • Cursor/connection context managers now rollback transaction on error, otherwise commit if autocommit=False #178 (thanks @julianit)

0.3.1 (2018-03-23)

  • Add after_create hook for connection configuration (thanks @lanfon72)

0.3.0 (2018-02-23)

  • Added optional pool connections recycling #167 (thanks @drpoggi)

0.2.0 (2017-06-24)

  • Fixed Cursor.execute returns a pyodbc.Cursor instead of itself #114

  • Fixed __aiter__ to not be awaitable for python>=3.5.2 #113

  • Tests now using aiodocker #106

0.1.0 (2017-04-30)

  • Fixed project version

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.3.2.tar.gz (18.5 kB view details)

Uploaded Source

File details

Details for the file aioodbc-0.3.2.tar.gz.

File metadata

  • Download URL: aioodbc-0.3.2.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3

File hashes

Hashes for aioodbc-0.3.2.tar.gz
Algorithm Hash digest
SHA256 4b1cf09b79efdf643489b1813e0edba92d8e5786c47255721623c4e4da1e7ab6
MD5 a92de73a9b1c4f7a4ea8a5ba62b67245
BLAKE2b-256 2fc61fff874b0947e0fa07b419899b0e75083c4eb691234b8eac92bae438eb0a

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