Skip to main content

Embeddable SQL database for Python — PostgreSQL-compatible, in-memory or persistent

Project description

petradb

Embeddable SQL database for Python — PostgreSQL-compatible, in-memory or persistent.

Wraps the PetraDB native shared library via ctypes. No compilation needed — just install the library and import.

Install

pip install petradb

Requires libpetradb-engine.so (Linux) or .dylib (macOS). Download from GitHub Releases and place in /usr/local/lib, or set PETRADB_LIB_PATH.

Quick Start

from petradb import Database

db = Database()  # in-memory

db.execute("""
    CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL, age INT);
    INSERT INTO users (name, age) VALUES ('Alice', 30), ('Bob', 25);
""")

for row in db.query("SELECT * FROM users ORDER BY name"):
    print(f"{row.id}: {row.name}, age {row.age}")

db.close()

Persistent Storage

db = Database('mydata.db')  # creates or opens

Context Manager

with Database() as db:
    db.execute("CREATE TABLE t (id INT)")
    rows = db.query("SELECT * FROM t")

Row Access

row = db.query_one("SELECT id, name FROM users WHERE id = 1")
row.name       # attribute access
row['name']    # dict-style access
row[0]         # index access
row.to_dict()  # {'id': 1, 'name': 'Alice'}

Cursor (Lazy Iteration)

with db.cursor("SELECT * FROM large_table") as cur:
    for row in cur:
        process(row)

User-Defined Functions

db.create_function('double', 1, lambda args: args[0] * 2)
db.query("SELECT double(age) FROM users")

Links

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

petradb-1.5.0.tar.gz (5.1 MB view details)

Uploaded Source

Built Distribution

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

petradb-1.5.0-py3-none-any.whl (5.2 MB view details)

Uploaded Python 3

File details

Details for the file petradb-1.5.0.tar.gz.

File metadata

  • Download URL: petradb-1.5.0.tar.gz
  • Upload date:
  • Size: 5.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for petradb-1.5.0.tar.gz
Algorithm Hash digest
SHA256 07e302b86fa5925fe90671ce223d01472f559a993d05fc1f3894f61519fdcd19
MD5 fc80d35eac6af9613a9604c4801f2fc4
BLAKE2b-256 def0eb56c7b02512825e2eb81fd2ca0da2f330b7423928dceab3f27ca3be84e3

See more details on using hashes here.

File details

Details for the file petradb-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: petradb-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for petradb-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f91ead7ca1b181c9895df0d35ae3f528219800c7f438eba047f5cb3ee6255dd5
MD5 d9edacc5ee01d172570c0d1361a12417
BLAKE2b-256 d9e3e1d2a72d278df7458014d382469330a0d0b264a9d3712466cc3b590be445

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