Awesome Open Source
Awesome Open Source


.. image:: :target: .. image:: :target: .. image:: :target: .. image:: :target: :alt: 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.


Mailing List:!forum/aio-libs

Chat room:

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.

.. code:: python

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()
    await cur.close()
    await conn.close()


Connection Pool

Connection pooling is ported from aiopg_ and relies on PEP492_ features:

.. code:: python

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()
        await cur.close()
        await conn.close()
    await pool.wait_closed()


Context Managers

Pool, Connection and Cursor objects support the context management protocol:

.. code:: python

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()



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


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


  • Python_ 3.5+
  • pyodbc_
  • uvloop_ (optional)

.. _Python: .. _asyncio: .. _pyodbc: .. _uvloop: .. _ODBC: .. _aiopg: .. _aiomysql: .. _PEP492: .. _unixODBC: .. _threads: .. _docker: .. _motor:

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
python (54,388
mysql (1,045
postgresql (743
sqlite (318
asyncio (280
oracle (127
async-await (90
odbc (21