Skip to main content

A set of db wrappers

Project description

Db Common Lib

Mainly for myself, switching between different database providers all the time, I wanted a consistent interface.
Thats what this is for.
More consistent (Think ADO.NET) db connectivity

Overview

Links psgsql, mysql and sqlite in a common interface
Unit tests provided to show usage

Usage

Connection string format is: <provider>://<username>:<password>@<host>/<database>
The supported providers are: sqlite, mysql, pgsql

with SessionFactory.connect(<connection string>) as session:
    id = session.execute_lastrowid("insert into test(name) values (:name);", {"name": "bob"})
    session.execute("update test set name = :name where id = :id", {"id": id, "name": "bob1"})

The session closes and commits automatically when it goes out of scope.
For more control, manually call commit and rollback.

The queries are db specific, but the mechanisms are the same for all.
Major differences are the different field types, the parameter specification and in the use of identity columns across the different db's.
Remember to add returning <field> to the insert statement for pgsql or you won't get the identity back.

Differences

sqlite

CREATE TABLE test(
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
    name TEXT
);

INSERT INTO test(name) VALUES (:name)

mysql

CREATE TABLE test(
    id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    name VARCHAR(50) NULL
);

INSERT INTO test(name) VALUES (%(name)s);

pgsql

CREATE TABLE test(
    id INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, 
    name VARCHAR(50) NULL
 );

INSERT INTO test(name) VALUES (%(name)s) RETURNING id;

Building

python -m build

Deploying

python -m twine upload --repository testpypi dist/*

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

sb_db_common-0.0.3.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

sb_db_common-0.0.3-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file sb_db_common-0.0.3.tar.gz.

File metadata

  • Download URL: sb_db_common-0.0.3.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for sb_db_common-0.0.3.tar.gz
Algorithm Hash digest
SHA256 d1a979d4e8555a14cc116f255badfc1cb6cf50fe91feb06ff04f3ebd0b756a1f
MD5 c231fab6c9349fc3993f1729c6a99508
BLAKE2b-256 7b5737693842a193a11128e63bcd0a459e8395e2e111565d270540dc3e32d956

See more details on using hashes here.

File details

Details for the file sb_db_common-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for sb_db_common-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 52876ef7bddb86cdb30bb588cdd1f8a052314703fe1e861d5bde621a050cc757
MD5 76ed1413a98cb729cbe0982121562d51
BLAKE2b-256 2f33123c71ebfadba2dbc3bec226a52c256a737dd854dbf17cfb0b6889822f63

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