Skip to main content

aioodbc

GitHub Actions status for master branch https://codecov.io/gh/aio-libs/aioodbc/branch/master/graph/badge.svg https://img.shields.io/pypi/v/aioodbc.svg https://img.shields.io/pypi/pyversions/aioodbc.svg Chat on Gitter

aioodbc is a Python 3.7+ 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. 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.

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


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

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


asyncio.run(test_example())

Connection Pool

Connection pooling is ported from aiopg and relies on PEP492 features:

import asyncio

import aioodbc


async def test_pool():
    dsn = "Driver=SQLite3;Database=sqlite.db"
    pool = await aioodbc.create_pool(dsn=dsn)

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


asyncio.run(test_pool())

Context Managers

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

import asyncio

import aioodbc


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

    async with aioodbc.create_pool(dsn=dsn) 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)


asyncio.run(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

To run tests locally without docker, install unixodbc and sqlite driver:

$ sudo apt-get install unixodbc
$ sudo apt-get install libsqliteodbc

Create virtualenv and install package with requirements:

$ pip install -r requirements-dev.txt

Run tests, lints etc:

$ make fmt
$ make lint
$ make test

Other SQL Drivers

  • aiopg - asyncio client for PostgreSQL

  • aiomysql - asyncio client form MySQL

Requirements

Changes

0.5.0 (2023-10-28)

  • Added support for python 3.12

  • Bumped minimal supported version of pyodbc to 5.0.1

  • Dropped aiodocker related testing to unlock python 3.12

0.4.1 (2023-10-28)

  • Implemented cursor setinputsizes.

  • Implemented cursor fetchval.

  • Added more type annotations.

  • Added autocommit setter for cusror.

0.4.0 (2023-03-16)

  • Fixed compatibility with python 3.9+.

  • Removed usage of explicit loop parameter.

  • Added default read size parameter for cursor.

  • Updated tests and CI scripts.

  • Code base formatted with black.

0.3.3 (2019-07-05)

  • Parameter echo passed properly in cursor #185

  • Close bad connections before returning back to pool #195

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.

Metadata

Release files for aioodbc 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aioodbc 0.5.0
File Size Uploaded
aioodbc-0.5.0.tar.gz 41.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aioodbc 0.5.0
File Interpreter ABI Platform
aioodbc-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 60.7 kB

Release files / aioodbc-0.5.0.tar.gz

Download URL aioodbc-0.5.0.tar.gz
Size 41.3 kB
Tags Source
SHA-256 checksum
How to use checksums
cbccd89ce595c033a49c9e6b4b55bbace7613a104b8a46e3d4c58c4bc4f25075
BLAKE2b-256 checksum
How to use checksums
45873a7580938f217212a574ba0d1af78203fc278fc439815f3fc515a7fdc12b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.13

Release files / aioodbc-0.5.0-py3-none-any.whl

Download URL aioodbc-0.5.0-py3-none-any.whl
Size 19.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bcaf16f007855fa4bf0ce6754b1f72c6c5a3d544188849577ddd55c5dc42985e
BLAKE2b-256 checksum
How to use checksums
b0804d1565bc16b53cd603c73dc4bc770e2e6418d957417e05031314760dc28c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.13

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page