Skip to main content

A Python package to execute SQL queries and procedures and manage backups.

Project description

sql_via_code

Overview

sql_via_code is a library for executing SQL queries, stored procedures, table backups, and reading queries from files, all with a clean and simple interface.


Features

  1. Query Execution: Execute SQL queries and fetch the results as a pandas DataFrame.
  2. Stored Procedure Execution: Run stored procedures with input parameters.
  3. Table Backup: Automatically back up a specified table's data before executing a query or procedure.
  4. Query from File: Read and execute SQL queries from external files to improve maintainability.
  5. Engine Management: Close all active database engines cleanly using close_engines.

Functions

get_query_from_db

Executes a SQL query and optionally backs up a table.

Parameters:

  • query (str): SQL query to execute.
  • table_to_backup (str or None): Table to back up. Pass None to skip the backup.
  • env_file_name (str, optional): Path to the .env file for database credentials (default is .env).
  • params (dict, optional): Query parameters.

Returns:

  • A pandas.DataFrame containing query results.

exec_procedure_from_db

Executes a stored procedure and optionally backs up a table.

Parameters:

  • procedure_name (str): Name of the stored procedure.
  • table_to_backup (str or None): Table to back up. Pass None to skip the backup.
  • env_file_name (str, optional): Path to the .env file for database credentials (default is .env).
  • params (dict, optional): Procedure parameters.

Returns:

  • A pandas.DataFrame containing procedure output.

get_query_from_file

Reads and executes an SQL query from a file.

Parameters:

  • file_path (str): Path to the .sql or .txt file containing the query.
  • table_to_backup (str or None): Table to back up before executing the query. Pass None to skip the backup.
  • env_file_name (str, optional): Path to the .env file for database credentials.
  • params (tuple or dict, optional): Parameters to bind to the SQL query.

Returns:

  • A pandas.DataFrame containing query results.

Example:

from sql_via_code import get_query_from_file

Read and execute a query from a file

result_df = get_query_from_file("queries/select_employees.sql", table_to_backup="employees") print(result_df)


close_engines

Closes all active database engines.

Important:
It is recommended to call this function at the end of your program to ensure that all database connections are closed properly and no resources are leaked.

Usage:

from sql_via_code import close_engines

Close all active database engines

close_engines()


Installation

To install the package and dependencies:

bash pip install sql_via_code


Dependencies

The project requires the following libraries:

  • pandas==2.2.3
  • SQLAlchemy==2.0.36
  • python-dotenv==1.0.1
  • pyodbc==5.2.0
  • aiofiles==24.1.0
  • tabulate==0.9.0
  • asyncpg==0.30.0 (for PostgreSQL)
  • aiomysql==0.2.0 (for MySQL)
  • aiosqlite==0.20.0 (for SQLite)

Environment Setup

Ensure you have a .env file in the root directory with the following database configuration:

plaintext DB_TYPE=your_db_type USER=your_username PASSWORD=your_password HOST=your_host NAME=your_database_name DRIVER=your_driver

Example .env file for MSSQL:

plaintext DB_TYPE=mssql USER=admin PASSWORD=secretpassword HOST=127.0.0.1 NAME=my_database DRIVER=ODBC Driver 17 for SQL Server

Example .env file for SQLite:

plaintext DB_TYPE=sqlite NAME=example.db


Usage Examples

Example 1: Executing a Query with Backup

python from sql_via_code import get_query_from_db

query = "SELECT * FROM employees" result_df = await get_query_from_db(query, table_to_backup="employees") print(result_df)


Example 2: Executing a Stored Procedure

python from sql_via_code import exec_procedure_from_db

procedure_name = "sp_get_employee_data" params = {"department": "Sales"} result_df = await exec_procedure_from_db(procedure_name, table_to_backup="employees", params=params) print(result_df)


Example 3: Reading Query from File

from sql_via_code import get_query_from_file, get_query_from_db, close_engines

query = get_query_from_file("queries/select_employees.sql", table_to_backup="employees") result_df = get_query_from_db(query, table_to_backup="employees") print(result_df)

Final step: Close all engines

close_engines()


Supported Databases

Database DB_TYPE Required Fields
SQLite sqlite NAME
PostgreSQL postgresql USER, PASSWORD, HOST, NAME
MySQL mysql USER, PASSWORD, HOST, NAME
MSSQL mssql USER, PASSWORD, HOST, NAME, DRIVER
Oracle oracle USER, PASSWORD, HOST, NAME

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

sql_via_code-0.2.6.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

sql_via_code-0.2.6-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file sql_via_code-0.2.6.tar.gz.

File metadata

  • Download URL: sql_via_code-0.2.6.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for sql_via_code-0.2.6.tar.gz
Algorithm Hash digest
SHA256 f2022e8deb5a13083605cb9c67f2bda06a2aad314d56d36bc315fbbeb3d92990
MD5 893c4524e768b46d11f82b6dab858b37
BLAKE2b-256 5301586a19dd27ab37340e51936e6c75792ac13eb9a0a4ce56e35099c14b3f80

See more details on using hashes here.

File details

Details for the file sql_via_code-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: sql_via_code-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for sql_via_code-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 006829540711d9a2ce999e2563708395f8e50d65d1728d39e7596aee1173c1cb
MD5 2eb74ef04bfe7f5b5b0deadfba9529cc
BLAKE2b-256 c89656011937693d3bbf7c5cb5a31d0ba44d66cf1893a3a4fe24f4946961e7fa

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