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.
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.
Install db-connector-kr via pip (once published to PyPI):
pip install db-connector-krPostgreSQL: 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- Database Connection
- Automatically handles database connection initialization and validation.
- Query Execution
- Retrieve results as pandas DataFrames (
read_to_df). - Execute DML queries such as INSERT, UPDATE, or DELETE (
execution_query).
- DataFrame Insertion
- Insert pandas DataFrames into database tables efficiently (
insert_df)
- 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)- Retrieve Data as DataFrame
query = "SELECT * FROM your_table;"
df = connector.read_to_df(query)
print(df.head())- Execute DML Queries
query = "INSERT INTO your_table (id, name) VALUES (1, 'Alice');"
connector.execution_query(query)- 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")- Close Connection
connector.close()- Support for Microsoft SQL Server (MSSQL).
- Support for Oracle.
- Enhanced error handling and logging.
Contributions are welcome! If you would like to contribute, please:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Submit a pull request with detailed information about your changes.
This project is licensed under the MIT License.
For any inquiries or issues, please create an issue in the repository or contact the maintainer directly.