Skip to content

lim39/DBConnector

Repository files navigation

db-connector-kr

db-connector-kr is a Python library designed to simplify database interactions across multiple DBMS platforms. It provides a consistent interface for executing queries, retrieving results as pandas DataFrames, and inserting data efficiently.

Supported DBMS

The following DBMS platforms are considered:

  • PostgreSQL (Supported)
  • Snowflake (Supported)
  • MySQL (Supported)
  • Microsoft SQL Server (MSSQL) (Planned)
  • Oracle (Planned)

Currently, only PostgreSQL, Snowflake, and MySQL are fully supported. MSSQL and Oracle support are planned for future updates.


Installation

Install db-connector-kr via pip (once published to PyPI):

pip install db-connector-kr

Optional Dependencies

PostgreSQL: To use PostgreSQL, install the additional dependency:

pip install db-connector-kr[postgres]

※※※If you encounter the error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 0: invalid start byte during installation, it might be due to an encoding issue in your Python runtime environment. Please try running the following command and then reinstall the db-connector-kr module:

pip install --only-binary psycopg2-binary psycopg2-binary

Features

  1. Database Connection
  • Automatically handles database connection initialization and validation.
  1. Query Execution
  • Retrieve results as pandas DataFrames (read_to_df).
  • Execute DML queries such as INSERT, UPDATE, or DELETE (execution_query).
  1. DataFrame Insertion
  • Insert pandas DataFrames into database tables efficiently (insert_df)

Usage

  1. Initialization
from db_connector.db_connector import DBConnector

# Initialize the connector for PostgreSQL
config = {
    "dbms": "pg",
    "user": "your_username",
    "pw": "your_password",
    "dbname": "your_database",
    "host": "localhost",
    "port": 5432
}
# Initialize the connector for maria, mysql
config = {
    "dbms": "mysql",
    "user": "test_user",
    "pw": "test_password",
    "dbname": "test_db",
    "host": "localhost",
    "port": 3306,
}

# Initialize the connector for snowflake
config = {
    "dbms": "sf",
    "user": "test_user",
    "pw": "test_password",
    "dbname": "test_db",
    "account": "test_account",
    "warehouse": "test_warehouse",
}

connector = DBConnector(**config)
  1. Retrieve Data as DataFrame
query = "SELECT * FROM your_table;"
df = connector.read_to_df(query)
print(df.head())
  1. Execute DML Queries
query = "INSERT INTO your_table (id, name) VALUES (1, 'Alice');"
connector.execution_query(query)
  1. Insert DataFrame into a Table
import pandas as pd

# Example DataFrame
data = {"id": [1, 2], "name": ["Alice", "Bob"]}
df = pd.DataFrame(data)

# Insert into the table
connector.insert_df(df, table="your_table")
  1. Close Connection
connector.close()

Planned Features

  • Support for Microsoft SQL Server (MSSQL).
  • Support for Oracle.
  • Enhanced error handling and logging.

Contributing

Contributions are welcome! If you would like to contribute, please:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Submit a pull request with detailed information about your changes.

License

This project is licensed under the MIT License.


Contact

For any inquiries or issues, please create an issue in the repository or contact the maintainer directly.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages