Skip to main content

python DB API wrapper (MySQL, PostgreSQL, SQLite)

Project description

The light Python DB API wrapper with some ORM functions (MySQL, PostgreSQL, SQLite)

Quick start

  1. pip install zzdb
  2. Visit https://github.com/AndreiPuchko/zzdb
  3. get forlders: database.docker, demo
  4. run up.sh in database.docker
  5. run any of .py files

Features:


Connect

from zzdb.db import ZzDb

database_sqlite = ZzDb("sqlite3", database_name=":memory:")

database_mysql = ZzDb(
    "mysql",
    database_name="zztest",
    host="0.0.0.0",
    port="3308",
    user="root",
    password="zztest"
)

database_postgresql = ZzDb(
    "postgresql",
    database_name="zztest1",
    host="0.0.0.0",
    port=5432,
    user="zzuser",
    password="zztest"
)

Define & migrate database schema (ADD COLUMN only).

zzdb.schema import ZzDbSchema

schema = ZzDbSchema()

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

zzdb-0.1.2.tar.gz (15.4 kB view hashes)

Uploaded Source

Built Distribution

zzdb-0.1.2-py3-none-any.whl (16.4 kB view hashes)

Uploaded Python 3

Supported by

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