ADBC Driver Manager for Python
The adbc-driver-manager package provides Python bindings for the Arrow Database Connectivity (ADBC) C API, serving as a generic entrypoint for ADBC drivers. It offers both low-level C API bindings and a higher-level, PEP 249 (DB-API 2.0) compatible interface. Currently at version 1.11.0, the library generally follows a monthly to bi-monthly release cadence, aligning with the broader Apache Arrow project.
Warnings
- gotcha The `adbc-driver-manager` package itself is primarily a low-level interface to the ADBC C API. For most application development, it is strongly recommended to use the DB-API 2.0 interface provided by specific ADBC driver packages (e.g., `adbc_driver_sqlite.dbapi.connect()`) which abstract away the manual loading of driver shared libraries.
- breaking Connections (`adbc_driver_manager.AdbcConnection` or `dbapi.Connection`) and Cursors (`dbapi.Cursor`) *must* be explicitly closed to avoid resource leaks in the underlying C library. Python's garbage collection is not guaranteed to call `__del__` in a timely manner.
- gotcha The `adbc-driver-manager` package requires a separately installed ADBC driver shared library (e.g., `libadbc_driver_sqlite.so`) to function. Unlike driver-specific packages (e.g., `adbc-driver-sqlite`) which bundle and load the driver transparently, when using `adbc-driver-manager` directly, you must manage the driver's availability (e.g., via `PATH` or `LD_LIBRARY_PATH`).
- gotcha The DB-API 2.0 interface and functionality for working with Arrow data (e.g., `fetch_arrow_table`, `adbc_ingest`) requires the `pyarrow` library to be installed. Without `pyarrow`, such features will be unavailable or raise errors.
- breaking Recent Python wheels for `adbc-driver-manager` (and other Apache Arrow libraries) have increased their minimum system requirements. Specifically, `manylinux_2_28` is now required for Linux wheels (up from `manylinux2010`), and macOS 12 is the minimum supported version for macOS wheels.
Install
-
pip install adbc-driver-manager -
pip install adbc-driver-manager pyarrow
Imports
- adbc_driver_manager
import adbc_driver_manager
- dbapi
import adbc_driver_sqlite.dbapi
Quickstart
import adbc_driver_sqlite.dbapi
import pyarrow
# Ensure the SQLite ADBC driver is installed for this example.
# pip install adbc-driver-sqlite pyarrow
try:
# Connect to a SQLite database using the DB-API 2.0 interface
# Use a context manager to ensure proper resource cleanup
with adbc_driver_sqlite.dbapi.connect() as conn:
with conn.cursor() as cur:
# Execute a query
cur.execute("CREATE TABLE IF NOT EXISTS test (id INTEGER, name TEXT)")
cur.execute("INSERT INTO test (id, name) VALUES (?, ?)", (1, 'Alice'))
cur.execute("INSERT INTO test (id, name) VALUES (?, ?)", (2, 'Bob'))
# Fetch results as an Arrow Table (an ADBC-specific extension)
cur.execute("SELECT id, name FROM test ORDER BY id")
table = cur.fetch_arrow_table()
print("Fetched Arrow Table:\n", table)
# Fetch results via standard DB-API row-oriented interface
cur.execute("SELECT id, name FROM test ORDER BY id")
row = cur.fetchone()
print(f"\nFetched one row: {row}")
rows = cur.fetchall()
print(f"Fetched remaining rows: {rows}")
# Bulk ingest data
new_data = pyarrow.table([[3, 4], ['Charlie', 'David']], names=['id', 'name'])
cur.adbc_ingest("test", new_data)
print(f"\nIngested {new_data.num_rows} rows.")
cur.execute("SELECT COUNT(*) FROM test")
count = cur.fetchone()[0]
print(f"Total rows after ingest: {count}")
except Exception as e:
print(f"An error occurred: {e}")