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 pypi 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.5.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

sb_db_common-0.0.5-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sb_db_common-0.0.5.tar.gz
  • Upload date:
  • Size: 6.9 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.5.tar.gz
Algorithm Hash digest
SHA256 c939f0fdfaaec6dc71977e2f74f5271a7e533c3c78a186c456a793ee091b0071
MD5 37e6a7c43f5f01c7d1aa277024938c63
BLAKE2b-256 6e3f05816d8972725b2e79374eac023275fa7d240969d17a5c8c192d9ee57189

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sb_db_common-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6e3b184e332235905fb1a7eec06d72535ca62ec4893b2a19ff754e82b27e0d77
MD5 c109df534767e5d5ab133e53f93271c3
BLAKE2b-256 3d096212c7db1c6f9da03dbb391a60526205d597fe0ff5b0b8684aaa43a9c74c

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