API Reference

SQLPyHelper

class sqlpyhelper.db_helper.SQLPyHelper(db_type: str | None = None, host: str | None = None, user: str | None = None, password: str | None = None, database: str | None = None, driver: str | None = None, port: str | None = None, oracle_sid: str | None = None)[source]

Bases: object

backup_table(table_name: str, backup_file: str) None[source]

Exports table data into a CSV file. Example: backup_table(‘users’, ‘users_backup.csv’)

begin_transaction() None[source]

Begin an explicit transaction. Works across all supported databases.

close() None[source]

Closes the cursor and database connection.

commit_transaction() None[source]

Commit the current transaction.

create_table(table_name: str, columns: dict[str, str]) None[source]

Creates a table dynamically using a dictionary format. Example: columns = {‘id’: ‘INTEGER PRIMARY KEY’, ‘name’: ‘TEXT’, ‘age’: ‘INTEGER’}

execute_query(query: str, params: tuple | None = None) None[source]

Executes a query with optional parameters

fetch_all() list[tuple][source]

Fetches all rows from the last executed query

fetch_by_param(table_name: str, column_name: str, value: Any) list[tuple][source]

Fetches rows from a table where a column matches the given value.

fetch_one() tuple | None[source]

Fetches a single row

get_connection_from_pool() Any[source]

Fetches a connection from the pool.

insert_bulk(table_name: str, data: list[dict[str, Any]]) None[source]

Inserts multiple rows at once. Example: data = [{‘id’: 1, ‘name’: ‘Alice’}, {‘id’: 2, ‘name’: ‘Bob’}]

insert_dynamic(table: str, data: dict[str, Any]) None[source]

Dynamically constructs and executes an INSERT query with database-specific placeholders.

reconnect() None[source]

Reconnects to the database if connection is lost

return_connection_to_pool(connection: Any = None) None[source]

Returns a connection back to the pool.

rollback_transaction() None[source]

Roll back the current transaction.

setup_connection_pool(min_conn: int = 1, max_conn: int = 5, pool_size: int = 5) None[source]

Sets up connection pooling based on the database type

Exceptions

class sqlpyhelper.db_helper.SQLPyHelperError[source]

Bases: Exception

Base exception for SQLPyHelper errors.

class sqlpyhelper.db_helper.ConnectionError[source]

Bases: SQLPyHelperError

Raised when a database connection fails.

class sqlpyhelper.db_helper.QueryError[source]

Bases: SQLPyHelperError

Raised when a query fails to execute.

class sqlpyhelper.db_helper.BackupError[source]

Bases: SQLPyHelperError

Raised when a backup operation fails.