Skip to main content

python DB API wrapper (MySQL, PostgreSQL, SQLite)

Project description

Python application

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

Quick start (run demo files)

- in docker:

git clone https://github.com/AndreiPuchko/q2db && cd q2db/database.docker
./up.sh
./down.sh

- on your system:

pip install q2db
git clone https://github.com/AndreiPuchko/q2db && cd q2db
# sqlite:
python3 ./demo/demo.py
# mysql and postgresql:
pip install mysql-connector-python psycopg2-binary
pushd database.docker && docker-compose up -d && popd
python3 ./demo/demo_mysql.py
python3 ./demo/demo_postgresql.py
pushd database.docker && docker-compose down -v && popd

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.12.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

q2db-0.1.12-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: q2db-0.1.12.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.0 Windows/10

File hashes

Hashes for q2db-0.1.12.tar.gz
Algorithm Hash digest
SHA256 c414174979e7467bba10474463f8418a186ce140122e20152034b7565f031055
MD5 03fb42d1583af197e411eca23f3a8762
BLAKE2b-256 b2182fc079b785f50f5085ded5faf973dea74fa08b7760680b7a0f573c8b04c3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: q2db-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.0 Windows/10

File hashes

Hashes for q2db-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 eb23af54b469c2e8e7a5ca4147ee3e7efac9059766fc0a9f712cd94223a14c86
MD5 743b275d7f3e3ff0059ab292cea637b3
BLAKE2b-256 d391cbfe09625758ff9e6e774095b43bafed823df10ca4fa4338003056eb5c09

See more details on using hashes here.

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