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
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
profileparameter (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,
error_if_missing=True
)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file db_bridge-0.3.7.tar.gz.
File metadata
- Download URL: db_bridge-0.3.7.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49e39caed303ae9af414b7f9ff2bb814a2eb93393489597c2998c3ec538a9437
|
|
| MD5 |
54ae4a5e2abc51c09c7c9bc3b6a2202a
|
|
| BLAKE2b-256 |
0ea60f221b7b6d72e6936e70945099a80f9dbba4d85719fba1f680546e7ce5b0
|
File details
Details for the file db_bridge-0.3.7-py3-none-any.whl.
File metadata
- Download URL: db_bridge-0.3.7-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6922ff03b4ab90eafb6ad9c15b18af596c169e6ae9c3e1c1f3e9c9c6d3918f99
|
|
| MD5 |
b9a08342353af8dbbf44dc43f953fa59
|
|
| BLAKE2b-256 |
6d5bc76ef7dc34a953857e738b7822dab645e0958fe94e123dbef14afd9fdfb0
|