Skip to main content

English | Chinese

py_dbtable_helper

py_dbtable_helper is an ultra-lightweight, dependency-free Python library designed to build SQL queries programmatically without the overhead of heavy ORMs.

Why py_dbtable_helper?

While full-stack ORMs like SQLAlchemy or Django ORM are powerful, they often introduce significant complexity and performance overhead. py_dbtable_helper follows a "SQL-First" philosophy:

  • Zero Dependencies: Pure Python standard library only.
  • SQL-Transparent: Generates clean, readable SQL strings that are easy to debug and optimize.
  • Security-Focused: Enforces parameterization by default to prevent SQL Injection attacks.

🎯 Core Features

  • Flexible Parameterization: Supports both named (:name) and pyformat (%(key)s) styles to ensure compatibility with any DB-API 2.0 driver (e.g., sqlite3, psycopg2, PyMySQL, ...).
  • AnyDict & AnyPair: A simple, intuitive API for defining WHERE clauses using nested dictionaries, returning a (sql, params) tuple ready for cursor.execute().

Configuration

You can switch the parameter placeholder style globally to match your database driver:

import py_dbtable_helper as table

# Default is NAMED (:name) - compatible with sqlite3, oracledb
# Switch to PYFORMAT (%(key)s) for psycopg2, PyMySQL, etc.
table.paramstyle = table.ParamStyle.PYFORMAT

Quick Start

Here is how to perform standard database operations:

import sqlite3
import py_dbtable_helper as table

# Setup
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
cursor.execute("CREATE TABLE users (id INTEGER, name TEXT, balance REAL)")

# 1. INSERT: Secure parameter binding
sql, params = table.insert("users", {"id": 1, "name": "Alice", "balance": 100.5})
cursor.execute(sql, params)

# 2. SELECT: Powerful WHERE clauses using AnyDict
# Supports _and, _or, IN, and standard comparisons
where_cond = {
    "_or": [
        {"balance": (">", 50)},
        {"id": ("IN", [1, 2, 3])}
    ]
}
sql, params = table.select("users", cols=["name"], where=where_cond)
print(cursor.execute(sql, params).fetchone())

# 3. UPDATE: Clean and safe updates
sql, params = table.update("users", {"balance": 250}, where={"id": ("=", 1)})
cursor.execute(sql, params)

# 4. DELETE: Precise data removal
sql, params = table.delete("users", where={"id": ("=", 1)})
cursor.execute(sql, params)

# 5. RETURNING
sql, params = table.insert("users", {"name": "Alan", "balance": 90})

# Appends a RETURNING clause to INSERT, UPDATE, or DELETE statements (supports sqlite3, psycopg2, and psycopg3)
# default: returning * ; spec fields, e.g. table.returning(sql, ['id','name'])
sql = table.returning(sql)
cursor.execute(sql, params)

🪄 How it works (The Technical Details)

  • AnyDict: A recursive dictionary structure used to build WHERE or HAVING clauses. It maps column names to operator-value tuples.
  • AnyPair: The library returns a tuple containing the SQL string and a dictionary of parameters, ensuring that your application code remains decoupled from the specific SQL construction logic.

⛱ Security & Robustness

py_dbtable_helper goes beyond simple parameterization to ensure your database operations are safe and resilient:

  • SQL Injection Prevention: All inputs are automatically converted to named placeholders (e.g., :w_id_0). This eliminates the risk of string-concatenation-based SQL injection.
  • Placeholder Safety: The library generates deterministic, unique placeholder names to prevent naming collisions when using the same column multiple times in a single query.
  • Defensive SQL Construction:
    • Empty IN Clauses: If you pass an empty list to an IN operator (e.g., {"id": ("IN", [])}), the library automatically generates 1=0 instead of a syntax-breaking IN (). This ensures your query returns an empty result set safely rather than crashing.
    • Empty WHERE Logic: If no conditions are provided, the library defaults to 1=1 to ensure valid SQL syntax, preventing unexpected syntax errors in your execution flow.

py_dbtable_helper — Simple, safe, and native SQL building for modern Python projects.

Metadata

Release files for py-dbtable-helper 1.0.1

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

Source distribution (sdist)

Source distribution for py-dbtable-helper 1.0.1
File Size Uploaded
py_dbtable_helper-1.0.1.tar.gz 19.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-dbtable-helper 1.0.1
File Interpreter ABI Platform
py_dbtable_helper-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 37.8 kB

Release files / py_dbtable_helper-1.0.1.tar.gz

Download URL py_dbtable_helper-1.0.1.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b14f5276124d4bd3543c0c1bef4ff480fcbc56d06580a9a901d63eec423d2f4c
BLAKE2b-256 checksum
How to use checksums
ca1c5b47125d0c94dd054c942cb69a5765477b2a0a943dfd9ef51569d7c45eca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / py_dbtable_helper-1.0.1-py3-none-any.whl

Download URL py_dbtable_helper-1.0.1-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc3192997ef91e1db2e460e9d952d37a3a71d07699ccbea604e38a06f70c25dd
BLAKE2b-256 checksum
How to use checksums
fa52c434e5b5405a23967e8bb4aca78ddc5e0fd9aab3dc065aefd872fbfdc585
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

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