Skip to main content

A simple SQL database helper package for Python.

Project description

SQLPyHelper

PyPI version PyPI downloads Python versions License: MIT GitHub stars

📌 SQLPyHelper v.0.1.4 🚀

A Python library for simplified database interactions across SQLite, PostgreSQL, MySQL, SQL Server, and Oracle. SQLPyHelper provides an intuitive API for handling queries, connection pooling, transactions, logging, and backups efficiently.

📖 Table of Contents


🚀 Features in v0.1.3

  • Unified connection pooling for multiple databases.
  • Automatic reconnection for lost connections.
  • Transaction support (BEGIN, ROLLBACK, COMMIT).
  • Secure parameterized queries to prevent SQL injection.
  • Bulk insertion & dynamic table creation.
  • Logging & error handling for better debugging.
  • CSV export & database backups.

📦 Installation

Install via PyPI:

pip install sqlpyhelper

📌 Package on PyPI: SQLPyHelper on PyPI

For local development:

git clone https://github.com/adebayopeter/sqlpyhelper.git
cd sqlpyhelper
pip install -r requirements.txt

⚙️ Setup Using .env

Create a .env file in your project root to manage database configurations securely by renaming .env_example.

# .env_example (Rename to .env)
DB_TYPE=postgres
DB_HOST=localhost
DB_USER=your_user
DB_PASSWORD=your_secure_password
DB_NAME=database_name
DB_DRIVER={ODBC Driver 17 for SQL Server}
ORACLE_SID=XE
ORACLE_DB_PORT=1521

Loading .env in Code

from dotenv import load_dotenv
import os

load_dotenv()
db_type = os.getenv("DB_TYPE")
host = os.getenv("DB_HOST")
user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
database = os.getenv("DB_NAME")

🛠 Usage Examples

Initialize SQLPyHelper

from sqlpyhelper.db_helper import SQLPyHelper
db = SQLPyHelper()  # Auto-detects database type based on `DB_TYPE`

SQLite Example

db.execute_query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
db.execute_query("INSERT INTO users (name) VALUES (?)", ("Alice",))
print(db.fetch_all()) # Expected Output: [(1, 'Alice')]
db.close()

PostgreSQL Example

db.execute_query("CREATE TABLE customers (id SERIAL PRIMARY KEY, name TEXT)")
db.execute_query("INSERT INTO customers (name) VALUES (%s)", ("Bob",))
db.begin_transaction()
db.execute_query("DELETE FROM customers WHERE name=%s", ("Bob",))
db.rollback_transaction()  # Undo delete

MySQL Example

db.execute_query("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100))")
db.execute_query("INSERT INTO users (id, name) VALUES (%s, %s)", (1, "Alice"))
print(db.fetch_by_param("users", "id", 1))  # Expected Output: [(1, 'Alice')]
db.close()

SQL Server Example

db.execute_query("CREATE TABLE orders (order_id INT PRIMARY KEY, item NVARCHAR(100))")
db.insert_bulk("orders", [{"order_id": 1, "item": "Laptop"}, {"order_id": 2, "item": "Mouse"}])
db.backup_table("orders", "orders_backup.csv")  # Export data to CSV

Oracle Example

db.execute_query("CREATE TABLE employees (id NUMBER PRIMARY KEY, name VARCHAR2(100))")
db.execute_query("INSERT INTO employees (id, name) VALUES (:1, :2)", (1, "Charlie"))
db.setup_connection_pool(min_conn=2, max_conn=10)  # Enable pooling for better performance
conn = db.get_connection_from_pool()
db.return_connection_to_pool(conn)

📂 Project Structure

📦 SQLPyHelper/
├─ sqlpyhelper/
│  ├─ __init__.py
│  └─ db_helper.py
├─ tests/
│  └─ test_sqlpyhelper.py
├─ .env_example
├─ .gitignore
├─ setup.py
├─ README.md
└─ requirements.txt

📌 Available Methods in SQLPyHelper

Method Description
execute_query(query, params=None) Executes a SQL query with optional parameters.
fetch_one() Retrieves a single row from query results.
fetch_all() Retrieves all rows from query results.
fetch_by_param(table, column, value) Fetches rows dynamically based on a given parameter.
create_table(table_name, columns_dict) Creates a table dynamically with a dictionary format.
insert_bulk(table, data_list) Inserts multiple rows at once efficiently.
backup_table(table, backup_file.csv) Exports table data to CSV format.
setup_connection_pool() Initializes database connection pooling.
get_connection_from_pool() Fetches a connection from the pool.
return_connection_to_pool(conn) Returns connection back to pool.
begin_transaction() Begins an explicit transaction.
rollback_transaction() Rolls back uncommitted transactions.
close() Closes the database connection safely.

🌍 Contributing

We welcome contributions from the open-source community! Follow these steps to contribute:

  1. Fork the repo: SQLPyHelper GitHub Repository
  2. Clone your fork:
    git clone https://github.com/adebayopeter/sqlpyhelper.git
    
  3. Create a new branch:
    git checkout -b feature-new-functionality
    
  4. Make changes, commit, and push:
    git commit -m "Added new feature"
    git push origin feature-new-functionality
    
  5. Submit a Pull Request!

☕ Support the Project

If you find SQLPyHelper useful, consider buying me a coffee to support continued development! Donate Here: PayPal

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

sqlpyhelper-0.1.4.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

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

sqlpyhelper-0.1.4-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file sqlpyhelper-0.1.4.tar.gz.

File metadata

  • Download URL: sqlpyhelper-0.1.4.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for sqlpyhelper-0.1.4.tar.gz
Algorithm Hash digest
SHA256 defe4b77dd38f11faff523bf735621687178973a823e5ed8cd722063a9df0170
MD5 3a83a85ae569c6cfbac4330b784f748e
BLAKE2b-256 1ad4ec17f8860fba957cff392ee767b2a64f576a9c29fe07a8b0060caa58f202

See more details on using hashes here.

File details

Details for the file sqlpyhelper-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: sqlpyhelper-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for sqlpyhelper-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c8d7f77fe230401769e59e53912f86a7251a8276debf070b3df2785dc8a22d8c
MD5 3ce83501d8635c6703fc6fd2299ee2a8
BLAKE2b-256 77701d2f4f2691388274a0f29d78d9c32d7326944dca8b4772bef122194d40aa

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