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.1.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.1-py3-none-any.whl (5.2 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: petradb-1.5.1.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.1.tar.gz
Algorithm Hash digest
SHA256 265909a51fe4ebf422a2ce30805cfa4343096c576b6e65c5ccd396f3f57aebad
MD5 ee13101eafc38860c27794152a99656a
BLAKE2b-256 19e00efc9e06406ab94b1bf26ded757dedaad4e93b7080b3cd322737e8ab23a0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: petradb-1.5.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc0ed501c0da2965e5c285acd12ea4b6c2985556c89bd879f579eed8fdaff550
MD5 6a61caa106519dd12c8a851167541917
BLAKE2b-256 f7ff9a2a19efc464809be26e62bf3e87d83f507059046e5eecaf4ef7e73c3688

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