Skip to main content

Unified Relational DB abstraction layer — clean adapters for 9+ databases

Project description

dd-db

Unified Relational DB abstraction layer for Python.

Connect to any relational database and get a pandas DataFrame back — with a consistent API for schema inspection, connection management, and query timing.

Supported Databases

Adapter Class Extra
SQLite (stdlib) SQLiteDB (none)
DuckDB DuckDB duckdb
PostgreSQL PostgresDB postgres
MySQL / MariaDB MySQLDB mysql
Snowflake SnowflakeDB snowflake
Google BigQuery BigQueryDB bigquery
ClickHouse ClickHouseDB clickhouse
SQL Server MSSQLDB mssql
Oracle OracleDB oracle

Install

pip install dd-db                   # SQLite only (stdlib, zero extra deps)
pip install "dd-db[duckdb]"         # + DuckDB
pip install "dd-db[postgres]"       # + PostgreSQL
pip install "dd-db[all]"            # all adapters
pip install "dd-db[dev]"            # dev tools + DuckDB

Quick Start

from dd_db import SQLiteDB

with SQLiteDB(":memory:") as db:
    db.run_query("CREATE TABLE t (id INT, name TEXT)")
    db.run_query("INSERT INTO t VALUES (1, 'Alice')")
    db.run_query("INSERT INTO t VALUES (2, 'Bob')")

    # SELECT returns a pandas DataFrame
    df = db.run_query("SELECT * FROM t")
    print(df)
    #    id   name
    # 0   1  Alice
    # 1   2    Bob

    # Parameterised queries
    row = db.run_query("SELECT * FROM t WHERE id = :id", params={"id": 1})

    # Schema inspection
    print(db.list_tables())          # ['t']
    print(db.describe("t"))          # DataFrame with column/type/pk columns
    schema = db.get_schema("t")      # TableSchema Pydantic model
    print(schema.row_count)          # 2

    # Timed query
    df, meta = db.timed_query("SELECT * FROM t")
    print(meta.execution_time_ms)    # e.g. 0.42

API Reference

Core methods (all adapters)

Method Returns Description
run_query(sql, params?) DataFrame Execute SQL; SELECT → rows, DML → {rows_affected}
list_tables(schema?) list[str] Table names
tables(schema?) DataFrame Table names as DataFrame
get_schema(table, schema?) TableSchema Typed column metadata
describe(table, schema?) DataFrame Human-readable column info
test_connection() bool Health check
connection_info() ConnectionInfo Loggable summary (no passwords)
timed_query(sql, params?) (DataFrame, QueryResult) Query + execution metadata
connect() None Open connection
disconnect() None Close connection

Context manager

with SQLiteDB("mydb.sqlite") as db:
    ...
# connection closed automatically

Pydantic models

from dd_db import TableSchema, ColumnInfo, QueryResult, ConnectionInfo
  • ColumnInfoname, data_type, nullable, default, primary_key
  • TableSchematable_name, schema_name, columns, row_count, full_name
  • QueryResultsql, rows_returned, columns, execution_time_ms, success, error
  • ConnectionInfoadapter, host, port, database, username

Examples

DuckDB analytics

from dd_db import DuckDB

with DuckDB() as db:
    df = db.run_query("""
        SELECT region, SUM(amount) AS total
        FROM sales
        GROUP BY region
        ORDER BY total DESC
    """)
    print(df)

PostgreSQL

from dd_db import PostgresDB

with PostgresDB(host="localhost", database="mydb", user="me", password="pw") as db:
    df = db.run_query("SELECT * FROM orders WHERE status = :status",
                      params={"status": "pending"})

Snowflake

from dd_db import SnowflakeDB

with SnowflakeDB(account="xy12345", user="me", password="pw",
                 database="ANALYTICS", schema="PUBLIC",
                 warehouse="COMPUTE_WH") as db:
    df = db.run_query("SELECT TOP 100 * FROM my_table")

Cookbooks

See cookbook/ for runnable examples:

  • 01_sqlite_basics.py — full walkthrough with SQLite (no install needed)
  • 02_duckdb_basics.py — analytics with DuckDB, including DataFrame joins

Running Tests

pip install -e ".[dev]"
python -m pytest

Tests use :memory: SQLite — no external server required.

Design

See docs/DESIGN.md for:

  • Why synchronous-first?
  • Why DataFrame return type everywhere?
  • Relationship to vanna.ai
  • How to add a new adapter

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dd_db-0.1.0.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dd_db-0.1.0-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file dd_db-0.1.0.tar.gz.

File metadata

  • Download URL: dd_db-0.1.0.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for dd_db-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ce0835a8b4c8381d422710a68683c7bb5ff2277ab52d4cc0613257ccf3105852
MD5 d2f3ac2b3921850c1c63093f063cc7e5
BLAKE2b-256 a93e371689855e2e81ac90b53f064e90dd5c421dd189a1629e3e09b944e03bed

See more details on using hashes here.

File details

Details for the file dd_db-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dd_db-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for dd_db-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e4be2ce8210ca37aa6fe4f78e37d8421848e8195cc011a8cfb59eff45de42266
MD5 0c4587203a630dac24588cec29469f41
BLAKE2b-256 9ed5184465b7c1928490bffd9e6c2c79e160fb1cd3009fb2cf74c3b1bd6a7049

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page