Skip to main content

A shapeshifting database adapter that seamlessly handles syntax differences between database systems

Project description

DbChimaera

PyPI version License: MIT

The Shapeshifting Database Adapter

DbChimaera is a lightweight and elegant Python library that provides a unified interface for working with multiple database backends. It automatically adapts query syntax between different database systems, allowing you to write your code once and run it with any supported database.

Features

  • Seamless Switching: Easily switch between SQLite, MySQL, and more without changing your query code
  • Automatic Syntax Adaptation: Write queries with one placeholder style, and let DbChimaera handle the translation
  • Connection Management: Simple, intuitive connection handling for different database types
  • Query Utilities: Convenient methods for common database operations

Installation

pip install dbchimaera

Quick Start

from dbchimaera import Chimaera

# Connect to SQLite (default)
db = Chimaera()

# Or connect to MySQL
mysql_config = {
    "host": "localhost",
    "user": "username",
    "password": "password",
    "database": "mydb"
}
db = Chimaera(mysql_config)

# Run queries with the same syntax regardless of database backend
users = db.execute_query("SELECT * FROM users WHERE age > %s", (25,))

# Insert data easily
user_data = {"name": "John Doe", "email": "john@example.com", "age": 30}
db.insert_data("users", user_data)

# Don't forget to close the connection when done
db.close_connection()

Detailed Usage

Creating a Connection

DbChimaera defaults to SQLite if no connection details are provided:

# Creates a connection to a local SQLite database named "database.db"
db = Chimaera()

For MySQL, provide a dictionary with connection details:

mysql_config = {
    "host": "your_server.com",
    "user": "your_username",
    "password": "your_password",
    "database": "your_database",
    "port": 3306  # Optional, defaults to 3306
}
db = Chimaera(mysql_config)

Executing Queries

DbChimaera provides a unified interface for executing queries:

# Select query with parameters
results = db.execute_query(
    "SELECT * FROM products WHERE category = %s AND price < %s",
    ("electronics", 500)
)

# Insert query
db.execute_query(
    "INSERT INTO logs (timestamp, message) VALUES (%s, %s)",
    (datetime.now(), "User logged in")
)

# Update query
affected_rows = db.execute_query(
    "UPDATE users SET last_login = %s WHERE id = %s",
    (datetime.now(), 42)
)

Inserting Data

The insert_data method provides a convenient way to insert dictionaries:

new_product = {
    "name": "Widget Pro",
    "category": "gadgets",
    "price": 29.99,
    "stock": 100
}

new_id = db.insert_data("products", new_product)
print(f"Inserted product with ID: {new_id}")

Query Parameters

Always use %s as your placeholder regardless of database type. DbChimaera will automatically translate to the appropriate syntax:

# This works with both SQLite and MySQL
db.execute_query("SELECT * FROM table WHERE id = %s", (123,))

API Reference

Class: Chimaera

Constructor

Chimaera(connection_details=None)
  • connection_details: Optional dictionary with database connection parameters. If None, connects to SQLite.

Methods

  • execute_query(query, params=None): Executes a query with optional parameters
  • query_with_params(query, params=None): Alias for execute_query with a more explicit name
  • insert_data(table, data): Inserts a dictionary of data into the specified table
  • adapt_query(query): Translates query placeholders to the correct format for the current database
  • close_connection(): Closes the database connection

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

dbchimaera-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

dbchimaera-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file dbchimaera-0.1.0.tar.gz.

File metadata

  • Download URL: dbchimaera-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for dbchimaera-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f656d2ca7eb089b254fca4e95d3e930013efba737caa397de95e36c61f60feef
MD5 a4e9591c5906cce751437d9415be1e74
BLAKE2b-256 4006e9f7e1c813e99db75c0e0b669e91de313d456402f61338d9d43a4947cdc7

See more details on using hashes here.

File details

Details for the file dbchimaera-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dbchimaera-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for dbchimaera-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a9d46c817933006d0249e318ffdef8a2fd4e6d7e116cfbf796496a8b297bf06
MD5 31b1aaeedd5e24908eb408898cbbf2e0
BLAKE2b-256 03cd43c383ae0d954ffbdb4a77b7dd5047ae81df64378803dedef430235d1357

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