Skip to main content

Db handler wrapper for interacting with a SQL database using SQLAlchemy.

Project description

tSQLoader

tSQLoader is a Python-based utility designed to simplify interactions with SQL databases using SQLAlchemy. This tool empowers users to manage and manipulate database records without requiring direct access to the production database or advanced SQL knowledge. It offers features such as seamless database connections, inserting or updating records using Pandas DataFrames, handling primary key conflicts gracefully, and verifying table existence. tSQLoader is particularly valuable for managing data pipelines, integrating data from various sources, and enabling a smoother, more accessible database interaction experience.

Features

  • Establishes a connection to SQL databases using SQLAlchemy.
  • Writes Pandas DataFrames to SQL tables with support for primary key conflict resolution.
  • Deletes conflicting rows in the database before inserting new data.
  • Checks if a specified SQL table exists.

Requirements

To use DatabaseHandler, ensure you have the following installed:

  • Python 3.7 or higher
  • SQLAlchemy
  • Pandas
  • A compatible SQL driver (e.g., pyodbc for MSSQL)

Build

python -m build

Installation

pip install .

Usage

1. Import and Initialize

Begin by importing the DatabaseHandler class and initializing it with your database connection details:

from database_handler import DatabaseHandler

db_handler = DatabaseHandler(
    server="server_address",
    database="database_name",
    username="username",
    password="password"
)

2. Writing Data to Table

import pandas as pd

# Create a sample DataFrame
data = {
    'id': [1, 2],
    'name': ['Alice', 'Bob']
}
df = pd.DataFrame(data)

# Write the DataFrame to the database
db_handler.write_to_db(df, table_name="sample_table", primary_keys=["id"])

Prerequisites

Before using the tSQLoader, you need to set up the required database and table. Use the following SQL script to create the table structure to replicate the table needed to execute the example above:

USE [SAMPLE_DB]
GO

/****** Table [dbo].[sample_table]   Script Date: 19.03.2025 ******/

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[sample_table](
    [id] [int] NOT NULL,
    [name] [varchar] (100) NULL
CONSTRAINT [PK_sample_table] PRIMARY KEY CLUSTERED
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

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

tsqloader-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

tsqloader-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file tsqloader-0.1.0.tar.gz.

File metadata

  • Download URL: tsqloader-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for tsqloader-0.1.0.tar.gz
Algorithm Hash digest
SHA256 44bf3bc2e1353b540b9386694a6ee38b465e9d621d7290ade8c6a07693d5b209
MD5 5bdc8f34b03fa9cb7a8afc44fc965088
BLAKE2b-256 aead0f175d93429aada70ffd53c2884e58c60f1ea238c6c3aa7dc840517ee087

See more details on using hashes here.

File details

Details for the file tsqloader-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tsqloader-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for tsqloader-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eba5c02c5724bd8ddb1d9864199f104ea8ae0b42b62f1fda853fce4051185e3f
MD5 bcd2ef94db586342035d7a2ecb2877f5
BLAKE2b-256 e30ceeb2efcdea2f4640bd17da95d3b3a300897c776b87e5bd0b24840244a82a

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