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
- 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 SQL queries from external files to improve maintainability.
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. PassNoneto skip the backup.env_file_name(str, optional): Path to the.envfile for database credentials (default is.env).params(dict, optional): Query parameters.
Returns:
- A
pandas.DataFramecontaining 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. PassNoneto skip the backup.env_file_name(str, optional): Path to the.envfile for database credentials (default is.env).params(dict, optional): Procedure parameters.
Returns:
- A
pandas.DataFramecontaining procedure output.
get_query_from_file
Reads a SQL query from a file.
Parameters:
file_path(str): Path to the.sqlor.txtfile containing the query.
Returns:
- A
strcontaining the SQL query.
Example:
from sql_via_code import get_query_from_file, get_query_from_db
query = get_query_from_file("queries/select_employees.sql")
result_df = await get_query_from_db(query, table_to_backup="employees")
print(result_df)
Installation
To install the package and dependencies:
pip install sql_via_code
Dependencies
The project requires the following libraries:
pandas==2.2.3SQLAlchemy==2.0.36python-dotenv==1.0.1pyodbc==5.2.0aiofiles==24.1.0tabulate==0.9.0asyncpg==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:
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:
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:
DB_TYPE=sqlite
NAME=example.db
Usage Examples
Example 1: Executing a Query with Backup
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
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
query = get_query_from_file("queries/select_employees.sql")
result_df = await get_query_from_db(query, table_to_backup="employees")
print(result_df)
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 |
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sql_via_code-0.2.3.tar.gz.
File metadata
- Download URL: sql_via_code-0.2.3.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21e1f34f0f2340d832f41969afc78272dc6940cecf54bc18287c892dc9db2fb2
|
|
| MD5 |
cb1aafe3ae97fd232186a58e8ac6d83e
|
|
| BLAKE2b-256 |
d3c075663be3d890d8d4cbc5a6c0ba467e7d368d3f31ee37603714b1dea4dc9a
|
File details
Details for the file sql_via_code-0.2.3-py3-none-any.whl.
File metadata
- Download URL: sql_via_code-0.2.3-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ed426073d1e3c4e47736ea9d5e70ea3d2eb4e8d425783470215998abeba6b77
|
|
| MD5 |
2c81ecf60c93f59968c91b162b2d70b8
|
|
| BLAKE2b-256 |
c408be9c20552dbcd7b1687f7c345f09cb7a007fc7c8ebd5cd8217fddbf842d3
|