aioodbc is a Python 3.7+ module that enables asynchronous access to ODBC databases using `asyncio`. It acts as an asynchronous wrapper around the `pyodbc` library, maintaining a similar API. The library internally uses threads to prevent blocking the event loop, a common strategy for integrating synchronous I/O operations into asynchronous applications. The current version is 0.5.0, with ongoing development focused on Python version compatibility, dependency updates, and API enhancements.
pip install aioodbcVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates creating an asynchronous connection pool, acquiring a connection, executing a query, inserting data, and fetching results using context managers for proper resource handling. Replace the placeholder DSN with your actual ODBC connection string. It also shows a basic `CREATE TABLE` and `INSERT` operation, followed by a `SELECT`.
Remove the `loop=asyncio.get_event_loop()` (or similar) argument from `aioodbc.connect()` and `aioodbc.create_pool()` calls. E.g., `await aioodbc.create_pool(dsn=dsn)` instead of `await aioodbc.create_pool(dsn=dsn, loop=loop)`.
Ensure your code does not rely on `Cursor.execute` returning a `pyodbc.Cursor` object. If you assigned the result of `execute` to a new variable and expected `pyodbc` specific methods, this would now be an `aioodbc.Cursor`.
Upgrade your `pyodbc` installation to version 5.0.1 or newer: `pip install --upgrade pyodbc`.
Always use `?` as placeholders for parameters in your SQL queries and pass a tuple or list of values to the `execute` method in the correct order. Example: `await cur.execute("INSERT INTO my_table (col1, col2) VALUES (?, ?);", (val1, val2))`.Always use `async with` for `aioodbc.create_pool`, `pool.acquire`, and `conn.cursor` to ensure proper resource management and automatic closing/releasing even in the presence of exceptions. If not using context managers, ensure `await cur.close()` and `await conn.close()` are called, ideally in `finally` blocks.
Install aioodbc using pip: `pip install aioodbc`.
Ensure the database connection is successful before creating a cursor. Check connection parameters and handle connection errors appropriately.
Use the correct method to create a cursor from the connection object: `cursor = conn.cursor()`.
Create a connection from the engine and then obtain a cursor: `conn = engine.connect(); cursor = conn.cursor()`.
Pass the connection object to the function, not the cursor: `df.to_sql('table_name', con=conn, schema='public', index=False)`.