Package: aioodbc
aioodbcis a Python 3.7+ module that makes it possible to accessODBCdatabases
withasyncio. It relies on the awesomepyodbclibrary and preserves the same look and
feel. Internallyaioodbcemploys threads to avoid blocking the event loop,threadsare not that as bad as you think!. Other drivers likemotoruse the
same approach.aioodbcis fully compatible and tested withuvloop. Take a look at the test
suite, all tests are executed with both the default event loop anduvloop.Basic Exampleaioodbcis based onpyodbcand provides the same api, you just need
to useyield from conn.f()orawait conn.f()instead ofconn.f()Properties are unchanged, soconn.propis correct as well asconn.prop = val.importasyncioimportaioodbcasyncdeftest_example():dsn="Driver=SQLite;Database=sqlite.db"conn=awaitaioodbc.connect(dsn=dsn)cur=awaitconn.cursor()awaitcur.execute("SELECT 42 AS age;")rows=awaitcur.fetchall()print(rows)print(rows[0])print(rows[0].age)awaitcur.close()awaitconn.close()asyncio.run(test_example())Connection PoolConnection pooling is ported fromaiopgand relies onPEP492features:importasyncioimportaioodbcasyncdeftest_pool():dsn="Driver=SQLite3;Database=sqlite.db"pool=awaitaioodbc.create_pool(dsn=dsn)asyncwithpool.acquire()asconn:cur=awaitconn.cursor()awaitcur.execute("SELECT 42;")r=awaitcur.fetchall()print(r)awaitcur.close()awaitconn.close()pool.close()awaitpool.wait_closed()asyncio.run(test_pool())Context ManagersPool,ConnectionandCursorobjects support the context management
protocol:importasyncioimportaioodbcasyncdeftest_example():dsn="Driver=SQLite;Database=sqlite.db"asyncwithaioodbc.create_pool(dsn=dsn)aspool:asyncwithpool.acquire()asconn:asyncwithconn.cursor()ascur:awaitcur.execute("SELECT 42 AS age;")val=awaitcur.fetchone()print(val)print(val.age)asyncio.run(test_example())InstallationIn a linux environmentpyodbc(henceaioodbc) requires theunixODBClibrary.
You can install it using your package manager, for example:$ sudo apt-get install unixodbc
$ sudo apt-get install unixodbc-devThen:pip install aioodbcRun testsTo run tests locally without docker, installunixodbcandsqlitedriver:$ sudo apt-get install unixodbc
$ sudo apt-get install libsqliteodbcCreate virtualenv and install package with requirements:$ pip install -r requirements-dev.txtRun tests, lints etc:$ make fmt
$ make lint
$ make testOther SQL Driversaiopg- asyncio client for PostgreSQLaiomysql- asyncio client form MySQLRequirementsPython3.7+pyodbcuvloop(optional)Changes0.5.0 (2023-10-28)Added support for python 3.12Bumped minimal supported version of pyodbc to 5.0.1Dropped aiodocker related testing to unlock python 3.120.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 #185Close bad connections before returning back to pool #1950.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 #114Fixed __aiter__ to not be awaitable for python>=3.5.2 #113Tests now using aiodocker #1060.1.0 (2017-04-30)Fixed project version0.0.4 (2017-04-30)Improved mysql testing0.0.3 (2016-07-05)Dockerize tests, now we can add more DBs to tests using docker #15, #17, #19Test suite executed with both default asyncio and uvloop #180.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.