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.5.tar.gz (13.0 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.5-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for db_bridge-0.3.5.tar.gz
Algorithm Hash digest
SHA256 969be9360d6b42bbdd8035b391ea8870a11ef39b01c1871b3eb26499f5aee32d
MD5 28e36ded586fa045ec7d56f36c34b9e4
BLAKE2b-256 051886b6eb63636ed1e90db13eacf0e0480c473b5c36690a63f20c84002e6c43

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for db_bridge-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 965a8ef3547a027e562338fcd50016dcbd8280bfff631703b005a48f0a742bf2
MD5 a46bada2df6aca48eb6335aa892c60d1
BLAKE2b-256 4bd745d61f5717c2bd3240d2d72bc5208e86d3d652aa9c9955a5d50572abb41e

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