Skip to main content

A Python package for simplified SQL operations across MSSQL, PostgreSQL, MySQL, and SQLite.

Project description

SQLPy

SQLPy (sql_pydb) is a Python library to bridge the gap between Python and Database Tables.

SQLPy



Instalation Guide

pip install sql_pydb
  • sql_pydb requires the following packages: Pandas, Numpy, pyodbc, python-dateutil

Overview:

  • You can connect to MSSQL and SQLite.
  • You can perform operations such as:
    • Create Database(s)
    • Create Table(s)
    • Drop Column(s)
    • Add Column(s)
    • Alter Column Types (If database supports it)
      • Performs a Try Cast check to be safe. (i.e. VARCHAR to INTEGER)
    • Read from Table
    • Delete from Table
    • Drop Table(s)

Usage

  1. Example: Upload Pandas DataFrame to a MSSQL Database
from sql_pydb.Database import DatabaseType 
from sql_pydb.Table import TableActions

table = TableActions(database=DATABASE, table_name=TABLE_NAME, 
                driver="ODBC Driver 17 for SQL Server",
                server="url,port",
                username="admin",
                password="admin",
                db_type=DatabaseType.MSSQL)

df = pd.read_csv("...file.csv")
table.identify_schema(df)   # Figures out the best column types. Only needed 1 time per TableActions instance
table.sync_schema(update_column_types=True, add_new_columns=True, delete_old_columns=False) # Optional
status = table.insert_df(df, batch=1_000)
assert status == True
  1. Example: Upload Pandas DataFrame to a SQLite Database
from sql_pydb.Database import DatabaseType 
from sql_pydb.Table import TableActions

table = TableActions(table_name=TABLE_NAME, 
                sqlite_path = SQLITE_PATH,  # "./path/file.db"
                db_type=DatabaseType.SQLITE)

df = pd.read_csv("...file.csv")
table.identify_schema(df)   # Figures out the best column types. Only needed 1 time per TableActions instance
table.sync_schema(update_column_types=True, add_new_columns=True, delete_old_columns=False) # Optional
status = table.insert_df(df, batch=1_000)
assert status == True
  1. Example: Query Table
table = TableActions(...)
# ...
command = table.generate_select_all_sql()
df = table.run_query(command)   # Returns pd.DataFrame
  1. Example: Execute Transactions
table = TableActions(...)
# ...
command1 = table.generate_add_columns_sql()
command2 = table.generate_drop_column_sql(column_name="UNUSED_COLUMN")
# ...
commandX = table.generate_insert_sql(df_new_records)
status = table.run_transaction([command1, command2, ..., commandX])
assert status == True
  1. Example: Create Table
from sql_pydb.Column import Column
from sql_pydb.Table import TableActions

table = TableActions(...)
# ...
user_defined_columns = [
    Column("FIRST_NAME", data_type="VARCHAR", size=100),
    ...
    Column("ADDRESS", data_type="VARCHAR"), # Default 255 for strings
]
table.identify_schema(df, user_defined_columns)
# Option 1: Let it automatically sync with the new defined columns
status = table.sync_schema(update_column_types=True, add_new_columns=True, delete_old_columns=False)
# Option 2: Manually create the table
command = table.generate_create_table_sql()
status = table.run_transaction([command])
assert status == True
  • You may choose to replace user_defined_columns with a pd.DataFrame with ["COLUMN", "DATA_TYPE", "SIZE"] columns. ["DATABASE", "TABLE"] columns may also be passed in.

Support:

  • If you have a question or need additional support, please create an issue ticket on SQLPy GitHub Repo.

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

sql_pydb-0.5.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sql_pydb-0.5.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file sql_pydb-0.5.0.tar.gz.

File metadata

  • Download URL: sql_pydb-0.5.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for sql_pydb-0.5.0.tar.gz
Algorithm Hash digest
SHA256 d804562b6e6bd47736d78f14b51641479cb2ed7fa11c6968e9676b421d0498eb
MD5 18c872409bced1b155ddead268fe5f72
BLAKE2b-256 cf0d19679cf1802366e5692674f0129e387b4e1b6f8ed2af7e8eda5747a2f4bf

See more details on using hashes here.

File details

Details for the file sql_pydb-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: sql_pydb-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for sql_pydb-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 49ec16ad4ae0b8863c6d4c4928f00e23a7cdf91c415990269520b1a4aaec4c7f
MD5 6fe9ff48edb8ac650e3931532ea67d79
BLAKE2b-256 65805915f0f4a02bf2ec6752975e543f5f30d748e7369e286fc8b6dc0b16032a

See more details on using hashes here.

Supported by

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