Skip to main content

presql

PostgreSQL and Psycopg2 wrapper.

Official Release

PreSQL can now be used on your Python projects through PyPi by running pip command on a Python-ready environment.

pip install presql --upgrade

Current version is 1.0.0, but more updates are coming soon. Installing it will also install required packages including psycopg2.

This is compatible with Python 3.9 or with the latest version.

package import

from presql import PreSQL

There are many ways to connect to a Postgres server, check Psycopg2 Connection documentation for more details. Some of those features may or may not be implement as of the current version.

Important! As always, do not store your credentials in the project folder.

Connect using a valid Database URI

with PreSQL(DATABASE_URI, sslmode="require") as db:
    print("\nConnect via URI", db.connected())

Connect using a valid DSN String

with PreSQL(f"dbname={DATABASE} user={USER} password={PASSWORD}") as db:
    print("\nConnect via DSN", db.connected())

Connect using parameters

with PreSQL(dbname=DATABASE, user=USER, password=PASSWORD, host=HOST, port=PORT) as db:
    print("\nConnect using parameters", db.connected())

Manual SQL commands

with PreSQL(dbname=DATABASE, user=USER, password=PASSWORD, host=HOST, port=PORT) as db:
    if db.connected():
        query = """
            CREATE TABLE IF NOT EXISTS inventory (
                inventory_id integer GENERATED ALWAYS AS IDENTITY NOT NULL,
                item varchar,
                total int,
                modified int); """
        db.execute(query)

Manual SQL commands

with PreSQL(dbname=dbname, user=user, password=password, host=host, port=port) as db:
    if db.connected():
        rows = db.execute("SELECT * FROM inventory LIMIT 5;").fetchall()
        for row in rows:
            print(" -", row["item"])

COUNT shortcut

with PreSQL(dbname=DATABASE, user=USER, password=PASSWORD, host=HOST, port=PORT) as db:
    if db.connected():
        count = db.count("inventory", where="item='ramen'")
        print("Count:", count)

SELECT shortcut

with PreSQL(dbname=DATABASE, user=USER, password=PASSWORD, host=HOST, port=PORT) as db:
    if db.connected():
        rows = db.select("inventory", "total", where="item='ramen'")
        for row in rows:
            print(row["total"])

INSERT Shortcut

with PreSQL(dbname=dbname, user=user, password=password, host=host, port=port) as db:
    if db.connected():
        count = db.count("tokens", where="token='ethereum'")
        if not count:
            db.insert("tokens", columns="token, usage, modified", values=f"('ethereum', 0, {modified})")

Auto-Commit = False, Rollback

with PreSQL(dbname=DATABASE, user=USER, password=PASSWORD, host=HOST, port=PORT, autocommit=False) as db:
    if db.connected():
        db.insert("inventory", columns="item, usage, modified", values=f"('jelly', 0, {modified})")
        # some failed transactions
        db.rollback()

UPDATE shortcut

with PreSQL(dbname=DATABASE, user=USER, password=PASSWORD, host=HOST, port=PORT) as db:
    if db.connected():
        total = 0
        rows = db.select("inventory", "total", where="item='ramen'")
        for row in rows:
            total = int(row["total"]) + 1
        db.update("inventory", columns=f"total={total}", where="item='ramen'")
        print("Usage:", total)

DELETE shortcut

with PreSQL(dbname=DATABASE, user=USER, password=PASSWORD, host=HOST, port=PORT) as db:
    if db.connected():
        db.insert("inventory", columns="item, total, modified", values=f"('soy milk', 0, {modified})")
        count = db.count("inventory", where="item='soy milk'")
        print("Anon:", count)
        
        db.delete("inventory", where="item='soy milk'")
        count = db.count("inventory", where="item='soy milk'")
        print("Anon:", count)

Metadata

Release files for presql 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for presql 1.0.2
File Size Uploaded Provenance Actions
presql-1.0.2.tar.gz 5.1 kB Without provenance Details for presql-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for presql 1.0.2
File Interpreter ABI Platform Actions
presql-1.0.2-py3-none-any.whl Python 3 none any Details for presql-1.0.2-py3-none-any.whl

Total release size: 10.4 kB

Release files / presql-1.0.2.tar.gz

Download URL presql-1.0.2.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
080fb72d1599187196124825c02028d6e889eb9d422a82513b45da62a1fc492f
BLAKE2b-256 checksum
How to use checksums
302061ec63453f8f65b2a3c82c39d6d4928ab0d8c1793358acaf1ebc1018d2f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release files / presql-1.0.2-py3-none-any.whl

Download URL presql-1.0.2-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
159b2dabe03c149beae7ead9b90cb862d91fbf3b3d3ca3414b69ba0f482b2a39
BLAKE2b-256 checksum
How to use checksums
bdb9cc25739cea3fc149124d15ced531bc3930358a7cb9cbc1c7ffdef8e280a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Security for presql 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page