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
- Query Execution: Execute SQL queries and fetch the results as a pandas DataFrame.
- Stored Procedure Execution: Run stored procedures with input parameters.
- Table Backup: Automatically back up a specified table's data before executing a query or procedure.
- Query from File: Read and execute SQL queries from external files to improve maintainability.
- 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.sqlor.txtfile containing the query.table_to_backup(str or None): Table to back up before executing the query. PassNoneto skip the backup.env_file_name(str, optional): Path to the.envfile for database credentials.params(tuple or dict, optional): Parameters to bind to the SQL query.
Returns:
- A
pandas.DataFramecontaining 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 |
Metadata
Release files for sql-via-code 0.3.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sql_via_code-0.3.6.tar.gz | 10.4 kB | Without provenance | Details for sql_via_code-0.3.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sql_via_code-0.3.6-py3-none-any.whl | Python 3 | none | any | Details for sql_via_code-0.3.6-py3-none-any.whl |
Total release size: 19.1 kB
Release files / sql_via_code-0.3.6.tar.gz
| Download URL | sql_via_code-0.3.6.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
547b4e91f37f3be571ed4844f3b8d0d035b0e14cdabac14d66b6772f27e328ba
|
|
BLAKE2b-256 checksum How to use checksums |
e078e235d7a39d54bee9d80f06cec4eb1fd7c5fa79d23b723fc2721f675012d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.6
|
Release files / sql_via_code-0.3.6-py3-none-any.whl
| Download URL | sql_via_code-0.3.6-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d334f967a11238fc39db3808ccbed00eaf49e2f231f1f0bad797f66cfea1b95d
|
|
BLAKE2b-256 checksum How to use checksums |
df6592a5d5dc9acdc1f97828755f3265f79e584d224bef468c473af4371f547a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.6
|
Security for sql-via-code 0.3.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.