Skip to main content

Minimal Python SQL connector with support for MySQL, SQLite, and PostgreSQL.

Project description

db-bridge

A minimal, flexible Python SQL connector for MySQL, SQLite, and PostgreSQL—just pass native SQL queries as strings, with optional INI profile configuration.


Table of Contents

  1. Installation
  2. Quick Start
  3. Configuration
  4. Usage Examples
  5. Testing
  6. Contributing
  7. License

Installation

Install from PyPI:

pip install db-bridge

For colorful output (needs colorfulPyPrint):

pip install db-bridge[color]

For local development:

git clone https://github.com/YourUsername/db-bridge.git
cd db-bridge
pip install -e .

Quick Start

from db_bridge.db_utils import run_sql
from db_bridge.config   import load_config

# Inspect your default config (INI default)
print(load_config())

# Simple SELECT (uses default profile)
rows = run_sql("SELECT id, name FROM users")
for row in rows:
    print(row)

# INSERT example
new_id = run_sql(
    "INSERT INTO users (name,email) VALUES (%s,%s)",
    params=("Alice","alice@example.com"),
)
print(f"Inserted row ID: {new_id}")

Configuration

Custom Config Override

If you ever need to point at a different INI file (for CI, Docker, or testing), set:

export DB_BRIDGE_CONFIG=/path/to/alternate_db_bridge.cfg

That file will be used only if it exists; otherwise ~/.db_bridge.cfg is loaded as usual.


INI Profiles (~/.db_bridge.cfg)

For multiple databases (MySQL, SQLite, PostgreSQL, or multiple instances of any), define profiles in:

[DEFAULT]
active = dev_db

[dev_db]
driver   = mysql
host     = localhost
port     = 3306
database = mydb_dev
user     = devuser
password = devpass

[prod_db]
driver   = mysql
host     = prod.db.example.com
port     = 3306
database = mydb_prod
user     = produser
password = prodpass

[sqlite_local]
driver   = sqlite
database = /full/path/to/local.db

[postgres_analytics]
driver   = postgres
host     = pg.example.com
port     = 5432
database = analytics
user     = pguser
password = pgpass
  • Default profile is the one named by [DEFAULT] active.
  • Callers can override at runtime with the profile parameter (see below).

Usage Examples

Simple Queries

# returns list of dicts by default
users = run_sql("SELECT id, username FROM users")
# as tuples
users_tuples = run_sql("SELECT id, username FROM users", as_dict=False)

Parameterized Queries (Safe)

# Always prefer parameterized queries to avoid SQL injection
email = "bob@example.com"
rows = run_sql(
    "SELECT id,name FROM users WHERE email = %s",
    params=(email,),
)

Multiple Profiles

Use the default profile:

run_sql("SELECT * FROM my_table")

Explicitly target another INI profile:

# MySQL production
run_sql("SELECT * FROM orders", profile="prod_db")

# SQLite local
run_sql("SELECT * FROM items", profile="sqlite_local")

# PostgreSQL analytics
run_sql("SELECT count(*) FROM events", profile="postgres_analytics")

Or supply a custom creds dict directly:

creds = {"driver":"sqlite","database":"/tmp/test.db"}
run_sql("SELECT * FROM foo", db_creds=creds)

Handling NULL values

When you pass raw SQL without parameters, you can convert Python None → SQL NULL:

# None in SQL literal becomes NULL
rows = run_sql(
    "SELECT * FROM orders WHERE shipped_date = None",
    none_to_null=True
)

Advanced Helpers

from db_bridge.db_utils import get_column_values, get_column_values_regexp

# Fetch single or multiple columns, prompt if multiple matches
val = get_column_values(
    "email", "status",
    table_name="users",
    unique_column_name="username",
    unique_column_value="alice",
    primary_key="id",
    as_tuple=False
)
print(val)

# Fetch rows matching a regex pattern
matches = get_column_values_regexp(
    "id", "username",
    table_name="users",
    unique_column_name="username",
    unique_column_regexp="^a.*"
)
for m in matches:
    print(m)

Testing

Run the test suite with pytest:

pytest tests/

Contributing

Contributions welcome! Please open issues or pull requests.
Follow existing style and add tests for new features.


License

MIT License © 2025 Kanad Rishiraj (RoamingSaint)

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

db_bridge-0.3.4.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

db_bridge-0.3.4-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file db_bridge-0.3.4.tar.gz.

File metadata

  • Download URL: db_bridge-0.3.4.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for db_bridge-0.3.4.tar.gz
Algorithm Hash digest
SHA256 477cdcbcecb6cc6ea851e805787f76a50e4bc24ea47b90c182df3205021a1130
MD5 8505f89d3c48a9844c8dcbcf25a07698
BLAKE2b-256 a9792640165a6ba94eb04db51a70e29e3fd00fbe6ea0e2ed6686f83146d32fd1

See more details on using hashes here.

File details

Details for the file db_bridge-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: db_bridge-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for db_bridge-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2d72d4c821aa481f4191b7ae4d74172fd1e1c05b35b4b07b668e320b26d07843
MD5 4bf6910c5004a5841efc6fa128f300f3
BLAKE2b-256 94d4669d2dc698574013633045e618f37f6d23ebe356233dbff04e06b7be61eb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page