Skip to main content

Package to simplify connections to SQL databases.

Project description

SQLconnect logo

PyPI Documentation Status License: MIT Code style: black

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SQLconnect-0.2.0.tar.gz (8.9 kB view hashes)

Uploaded Source

Built Distribution

SQLconnect-0.2.0-py3-none-any.whl (10.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page