Package to simplify connections to SQL databases.
Project description
SQLconnect is a Python package designed to simplify the process of connecting to SQL databases. It uses a connections.yaml
file for database configuration and a .env
file for secure credentials management. SQLconnect supports executing SQL from .sql
files or Python strings, and retrieving data into pandas DataFrames. This package is particularly useful for data analysts and developers who need a straightforward way to interact with SQL databases.
Features
-
Allows easy configuration and management of database connections using a
connections.yaml
file. -
Supports the use of a
.env
file for secure storage of database credentials, enhancing security. -
Leverages SQLAlchemy for database connections, providing a robust and flexible framework for SQL operations.
-
Enables executing SQL queries and directly retrieving the results into pandas DataFrames, facilitating easy data manipulation and analysis.
-
Capable of handling multiple database connections, allowing users to switch between different databases as needed.
-
Provides functionality to execute SQL queries either by reading from an external file or directly from a string.
Installation
pip install SQLconnect
Configuration
To use SQLconnect, create a connections.yaml
file in the root of your project (or in your home directory) with the following example structure:
connections:
Database_PROD:
sqlalchemy_driver: 'mssql+pyodbc'
odbc_driver: 'SQL+Server'
server: 'prod-server.database.com'
database: 'ProdDB'
username: '${DB_PROD_USERNAME}' # References DB_PROD_USERNAME in .env
password: '${DB_PROD_PASSWORD}' # References DB_PROD_PASSWORD in .env
options:
- 'Trusted_Connection=No'
Also create a .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
DB_PROD_USERNAME=prodUser
DB_PROD_PASSWORD=prodPassword
Replace the example values with your actual database connection details.
Usage
Here's a quick example to get you started:
import SQLconnect as sc
# Set up a database connection, all configuration is handled with connections.yaml and .env
connection = sc.SQLconnector("Database_PROD")
# Assign the results of a query to a pandas DataFrame
df = connection.sql_to_df("your_query.sql")
# Explore the dataframe with pandas
print(df.describe())
Documentation
Full documentation for SQLconnect can be found at https://sqlconnect.readthedocs.io/
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.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b60852a5e1d9bbb0b8a2cd41cc7a1f83da20dac2b0b5f0b2bef7f3d39c9bd675 |
|
MD5 | 330b88630ea1ff3b88ac787c44d0d240 |
|
BLAKE2b-256 | dabc33590fdedfc679e6067d7630021d22197ffb30bc21ab77053f46cd5b2d4a |