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

The native library is bundled in the wheel. Currently supports Linux x86_64 only. macOS and ARM support coming soon.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: petradb-1.5.2.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.2.tar.gz
Algorithm Hash digest
SHA256 c8ab66dbf3ac35c5b9b8beea7a4eb2cfacccd0f5f0e2e2e79819d461bce599bc
MD5 bd3d630b023dcd75b44775c7415f2b3d
BLAKE2b-256 6ea721157826fbb912c1da7e531f97ed99aa572f7f76010b08c47dcca78420d7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: petradb-1.5.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3a7e2beda54cbdf7ee67a443dfce80f49f111383b5db70207332af93d4077cb1
MD5 2984d80d6cd6becbca03b27dee21bd21
BLAKE2b-256 93d0dd53765312ed4c4388867252309711030d0271b0a3a7a5ba7c11eb098a9e

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