Simplifies connections to SQL databases for data analysts. Populate DataFrames with the results of queries directly from .sql files.
Project description
SQLconnect is a Python package designed to provide a straightforward way to interact with SQL databases (Postgres, Microsoft SQL Server, Oracle ect.). It enables direct population of DataFrames from .sql files. A sqlconnect.yaml
file is used for database configuration and a sqlconnect.env
file for secure credentials management.
Features
-
Turn SQL queries into DataFrames in as few as 3 lines of code
-
Easy management of multiple database connections using a configuration file
-
Secure storage of database credentials using environment variables
-
Execute SQL queries and commands directly from .sql files or from a string
-
Integration with SQLAlchemy & Pandas providing robust and flexible SQL operations
import sqlconnect as sc
connection = sc.Sqlconnector("My_Database") # Set up a database connection based on sqlconnect.yaml
df = connection.sql_to_df("path/to/sql_query.sql") # Assign the results of a query to a DataFrame
print(df.describe()) # Explore the DataFrame with Pandas
Configuration
To use SQLconnect, create a sqlconnect.yaml
file in the root of your project (or in your home directory) with the following example structure:
connections:
My_Database:
dialect: 'mssql'
dbapi: 'pyodbc'
host: 'prod-server.example.com'
database: 'master'
username: '${MSSQL_USERNAME}' # References MSSQL_USERNAME in sqlconnect.env
password: '${MSSQL_PASSWORD}' # References MSSQL_PASSWORD in sqlconnect.env
options:
driver: 'ODBC Driver 17 for SQL Server'
A_Postgres_Database:
dialect: 'postgresql'
dbapi: 'psycopg2'
host: 'dbserver123.company.com'
database: 'postgres'
username: '${POSTGRES_USERNAME}'
password: '${POSTGRES_PASSWORD}'
Also create a sqlconnect.env
file in the root of your project (or in your home directory) with the following example structure:
# This file should be kept secure and not checked into version control (add to .gitignore)
# Production Database Credentials
MSSQL_USERNAME=prodUsername
MSSQL_PASSWORD=actualprodPassword
POSTGRES_USERNAME=postgresProdUsername
POSTGRES_PASSWORD=actualprodPassword
Replace the example values with your actual database connection details.
Documentation
Full documentation for SQLconnect can be found at https://sqlconnect.readthedocs.io/
Installation
pip install sqlconnect
License
This project is licensed under the 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
Hashes for sqlconnect-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6129251bc0a6731b64e93d79239477ef59ae1ff4303a8aa9f41e68cd67cf85a5 |
|
MD5 | 85972ba502b4bfe387d1496287783c82 |
|
BLAKE2b-256 | bf1c3d85771b8b715d02268c68f0b328503bda7e655ee3a3b1bb010e9935fb28 |