ADBC Driver Manager for Python

1.11.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates connecting to a SQLite database using the recommended DB-API 2.0 interface provided by `adbc_driver_sqlite.dbapi`. It shows how to execute queries, fetch results as both Arrow Tables and standard Python rows, and perform bulk data ingestion. Ensure `adbc-driver-sqlite` and `pyarrow` are installed for this example to run. Connections and cursors should always be managed with context managers or explicitly `close()`d to prevent resource leaks.

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}")

view raw JSON →