Skip to main content

A python speech to text library

Project description

SQLite DB Class

A simple and flexible SQLite database wrapper class to simplify common database operations like INSERT, UPDATE, DELETE, SELECT, and more. Supports complex queries including JOIN, GROUP BY, HAVING, and transactions, with easy-to-use methods and error handling.

Key Features

  • Create Tables: Create tables with automatic checks for existing tables.
  • Insert, Update, Delete: Perform basic CRUD operations with minimal syntax.
  • Custom Queries: Execute any custom SQL query with optional parameters.
  • Join Support: Easily join tables using INNER, LEFT, RIGHT, and FULL OUTER joins.
  • Group By and Aggregations: Perform GROUP BY queries with optional HAVING clauses.
  • Transactions: Execute a set of queries as a transaction for consistency.
  • Error Handling: Returns error messages instead of raising exceptions.

Installation

You can install this package from PyPI:

pip install sqlite-db-wrapper

Usage Examples

  1. Creating a Table

    Create a table with specified columns if it does not already exist.

    from db_wrapper import DB
    
    # Initialize database connection
    db = DB("example.db")
    
    # Define table columns and types
    columns = {
        "id": "INTEGER PRIMARY KEY",
        "name": "TEXT",
        "age": "INTEGER"
    }
    
    # Create the table
    db.create_table("users", columns)
    
  2. Inserting Data

    Insert data into the table.

    # Insert a single user
    db.insert("users", {"name": "John Doe", "age": 30})
    
    # Insert another user
    db.insert("users", {"name": "Jane Smith", "age": 25})
    
  3. Selecting Data

    Select data from the table.

    # Fetch all users
    users = db.read_all("users")
    print(users)
    
    # Fetch user by name
    user = db.read_one("users", "name = ?", ("John Doe",))
    print(user)
    
  4. Updating Data

    Update data in the table.

    # Update the age of a user
    db.update("users", {"age": 31}, "name = ?", ("John Doe",))
    
  5. Deleting Data

    Delete data from the table.

    # Delete a user by name
    db.delete("users", "name = ?", ("John Doe",))
    
  6. Performing Joins

    Join two tables with a specified condition.

    # Assuming there are two tables: 'users' and 'orders'
    # Perform an INNER JOIN between 'users' and 'orders'
    join_condition = "orders.user_id = users.id"
    result = db.join("users", "orders", join_condition, join_type="INNER", columns=["users.name", "orders.total"])
    print(result)
    
  7. Using Group By and Aggregations

    Perform GROUP BY operations with an optional HAVING clause.

    # Group by age and filter groups with more than 1 user
    result = db.group_by("users", ["age"], having="COUNT(*) > 1")
    print(result)
    
  8. Running Custom Queries

    Execute any custom query with parameters.

    # Run a custom SELECT query with a parameter
    result = db.custom_query("SELECT name, age FROM users WHERE age > ?", (30,))
    print(result)
    
  9. Running Transactions

    Execute a set of queries as a single transaction.

    queries = [
        "INSERT INTO users (name, age) VALUES ('Alice', 22)",
        "UPDATE users SET age = 23 WHERE name = 'Alice'"
    ]
    
    # Execute the queries as a transaction
    db.transaction(queries)
    

Methods Overview

  • create_table(table: str, columns: dict): Creates a table if it does not already exist.

  • insert(table: str, data: dict): Inserts a row into the specified table.

  • update(table: str, data: dict, where: str, where_params: tuple): Updates rows in the table with a WHERE clause.

  • delete(table: str, where: str, where_params: tuple): Deletes rows from the table with a WHERE clause.

  • read_all(table: str): Retrieves all rows from the specified table.

  • read_one(table: str, where: str, params: tuple): Retrieves a single row from the table using a WHERE clause.

  • custom_query(query: str, params: tuple): Executes a custom SQL query with optional parameters.

  • join(table1: str, table2: str, on_condition: str, join_type: str, columns: list): Performs a JOIN operation between two tables.

  • group_by(table: str, columns: list, having: str): Performs a GROUP BY operation with an optional HAVING clause.

  • transaction(queries: list): Executes a set of queries as a transaction. close(): Closes the database connection.

Error Handling

Instead of raising exceptions, all methods return a dictionary with an "error" key in case of an error, e.g.:

{
    "error": "Error message"
}

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

sqlsupport-0.0.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

SqlSupport-0.0.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file sqlsupport-0.0.2.tar.gz.

File metadata

  • Download URL: sqlsupport-0.0.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for sqlsupport-0.0.2.tar.gz
Algorithm Hash digest
SHA256 7989b058152c074f86ec0b499cf3b82f63cb7e7995e227073745b979b33cdb55
MD5 af61a21b7082430953905369956a5504
BLAKE2b-256 25f013772204625fcce819a32580fcb748a7de4d65c93c85ae00c0dedd257ef7

See more details on using hashes here.

File details

Details for the file SqlSupport-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: SqlSupport-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for SqlSupport-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ebee00d10151ee6dc81857d2d4b8558adf08c487fbd9244b8a5521ff8044d944
MD5 075c9732e84971359a69d7f943dedf6f
BLAKE2b-256 cd3d186ce29ac9c1b770aa2e3125fb27f2f43e3084b2e6ade33c3f86b7188b78

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