Skip to main content

python DB API wrapper (MySQL, PostgreSQL, SQLite)

Project description

Python application

Q2DB: Lightweight Python DB API Wrapper & ORM

Q2DB is a lightweight Python library that wraps standard DB API connectors for MySQL, PostgreSQL, and SQLite, providing a simple ORM-like interface for database operations. It enables easy schema definition and migration, supports basic CRUD operations, and offers a convenient cursor abstraction for querying and manipulating data. Q2DB is designed for rapid prototyping, small projects, and educational purposes, focusing on simplicity and minimal configuration.

Key features include:

  • Unified API for MySQL, PostgreSQL, and SQLite
  • Schema definition and migration (ADD COLUMN only)
  • Simple ORM-like CRUD operations (insert, update, delete, get)
  • Foreign key checks and logging of changes
  • Cursor abstraction for flexible queries and record navigation
  • Docker support for quick setup and testing

Features:


Connect

from q2db.db import Q2Db

database_sqlite = Q2Db("sqlite3", database_name=":memory:")
# or just
database_sqlite = Q2Db()


database_mysql = Q2Db(
    "mysql",
    user="root",
    password="q2test"
    host="0.0.0.0",
    port="3308",
    database_name="q2test",
)
# or just
database_mysql = Q2Db(url="mysql://root:q2test@0.0.0.0:3308/q2test")

database_postgresql = Q2Db(
    "postgresql",
    user="q2user",
    password="q2test"
    host="0.0.0.0",
    port=5432,
    database_name="q2test1",
)

Define & migrate database schema (ADD COLUMN only).

q2db.schema import Q2DbSchema

schema = Q2DbSchema()

schema.add(table="topic_table", column="uid", datatype="int", datalen=9, pk=True)
schema.add(table="topic_table", column="name", datatype="varchar", datalen=100)

schema.add(table="message_table", column="uid", datatype="int", datalen=9, pk=True)
schema.add(table="message_table", column="message", datatype="varchar", datalen=100)
schema.add(
    table="message_table",
    column="parent_uid",
    to_table="topic_table",
    to_column="uid",
    related="name"
)

database.set_schema(schema)

INSERT, UPDATE, DELETE

database.insert("topic_table", {"name": "topic 0"})
database.insert("topic_table", {"name": "topic 1"})
database.insert("topic_table", {"name": "topic 2"})
database.insert("topic_table", {"name": "topic 3"})

database.insert("message_table", {"message": "Message 0 in 0", "parent_uid": 0})
database.insert("message_table", {"message": "Message 1 in 0", "parent_uid": 0})
database.insert("message_table", {"message": "Message 0 in 1", "parent_uid": 1})
database.insert("message_table", {"message": "Message 1 in 1", "parent_uid": 1})

# this returns False because there is no value 2 in topic_table.id - schema works!
database.insert("message_table", {"message": "Message 1 in 1", "parent_uid": 2})


database.delete("message_table", {"uid": 2})

database.update("message_table", {"uid": 0, "message": "updated message"})

Cursor

cursor = database.cursor(table_name="topic_table")
cursor = database.cursor(
    table_name="topic_table",
    where=" name like '%2%'",
    order="name desc"
)
cursor.insert({"name": "insert record via cursor"})
cursor.delete({"uid": 2})
cursor.update({"uid": 0, "message": "updated message"})

cursor = database.cursor(sql="select name from topic_table")

for x in cursor.records():
    print(x)
    print(cursor.r.name)

cursor.record(0)['name']
cursor.row_count()
cursor.first()
cursor.last()
cursor.next()
cursor.prev()
cursor.bof()
cursor.eof()

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

q2db-0.1.60.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

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

q2db-0.1.60-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file q2db-0.1.60.tar.gz.

File metadata

  • Download URL: q2db-0.1.60.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.11.7 Windows/10

File hashes

Hashes for q2db-0.1.60.tar.gz
Algorithm Hash digest
SHA256 92a53837b9ad0f4b83a81807f16d42ac0f2214ec9ed3e1bed915bff16673f15a
MD5 376364d76393834359450240ee971cf9
BLAKE2b-256 ce357356a00d6e2a26c32fdf3c842352efcc4677e53dd50d739c19e34b54e247

See more details on using hashes here.

File details

Details for the file q2db-0.1.60-py3-none-any.whl.

File metadata

  • Download URL: q2db-0.1.60-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.11.7 Windows/10

File hashes

Hashes for q2db-0.1.60-py3-none-any.whl
Algorithm Hash digest
SHA256 742607bccae92c729583915d2fa4f962afacd3275b18f1242155fc2e40f55dc0
MD5 bc1b06d7dc05be3898c08faa484937f3
BLAKE2b-256 326156e1d092d23bbed3aac733ca63712bc8155cad71d9b7cfcb7c96104dff16

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