Skip to main content

数据库操作工具包

Project description

database-smartools

介绍

一个功能全面的数据库操作工具包,提供统一接口支持多种数据库系统,简化数据库连接、查询与管理操作。支持连接池管理、事务处理和跨数据库兼容功能,适用于需要与多数据库交互的Python项目。

软件架构

  • 核心模块
    • database/db_handler.py:数据库连接管理核心类
    • database/*_extend.py:各数据库类型的扩展实现(MySQL、Oracle、PostgreSQL等)
    • utils/:配置解析、日志记录等辅助工具
  • 技术栈:Python 3.11+、SQLAlchemy、DBUtils连接池

支持数据库类型

  • MySQL
  • Oracle
  • PostgreSQL
  • 达梦数据库(Dameng)
  • OceanBase

安装教程

pip install database-smartools

使用说明

# 基本使用示例
from database_smartools.database import db_handler as dh
from database_smartools.utils import logger, output
import pandas as pd
import traceback

    if args["db_key"] not in dh.POOL_MAP.keys():
        dh.get_pool_by_key(args["db_key"])

    pool_map = dh.POOL_MAP[args["db_key"]]

    params = {
        "key": "value"
    }

    conn_map, message = dh.get_conn_map(pool_map)
    if not conn_map:
        result = False
        return output.OutputUtil.map(result=result, message=message)

    try:
        conn = conn_map["conn"]
        # conn_map["db_type"] = "oceanbase"
        # print(conn_map)
        # 日期遍历
        result = True
        logger.info("开始执行,执行参数:")
        logger.info(params)
        df, message = dh.select_by_map(conn_map, "select 'database_smartools' as software from dual", params)
        df = pd.DataFrame(df["data"])
        if not r:
            result = False

    except Exception as e:
        conn.rollback()  # 回滚事务
        # 获取完整堆栈的错误信息
        message = f"接口执行失败!\n完整错误堆栈信息:\n{traceback.format_exc()}"
        logger.error(message)
        result = False
    finally:
        conn.commit()
    dh.close_conn(pool_map, conn_map)
    return output.OutputUtil.map(result=result, message=message)

Debug示例

# 初始化数据库连接
from functions import t_r_299_result_n
from database_smartools.utils import debug

if __name__ == "__main__":
    params = {
        "db_key": "GB_YFDW_DW",
        "p_jgdm": "70380000",
        "p_file_date": "20240101",
        "ind_type": "D",
        "p_data_guid": "test_cqb",
        "p_fund_type": "08",
        "p_fund_category": "01",
        "p_item_table": "",
        "p_fdate": "20240101"
    }
    debug.debug_script(t_r_299_result_n.t_r_299_result_n, params, 'dev')

项目依赖

  • SQLAlchemy 2.0.40+
  • DBUtils 3.0.0+
  • 各数据库驱动(mysql-connector-python、oracledb等)

许可证

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

database_smartools-0.0.4.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

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

database_smartools-0.0.4-py3-none-any.whl (36.3 kB view details)

Uploaded Python 3

File details

Details for the file database_smartools-0.0.4.tar.gz.

File metadata

  • Download URL: database_smartools-0.0.4.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for database_smartools-0.0.4.tar.gz
Algorithm Hash digest
SHA256 830e5d79ca2ef722c5119d0798f99d02997f425b71201ce9133cf739c535e41c
MD5 f083bd1600f02215932c30ca3657a759
BLAKE2b-256 70c2e1128ade4e77e6d4a545e5f88c29f93b3c6e811b5dc4cece6b41233afcb9

See more details on using hashes here.

File details

Details for the file database_smartools-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for database_smartools-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 33515f258972cc91754c8b14bc276fa7165926a53a278a64e8fa31085fb22c22
MD5 470d19f44726dd3b88383ebe342a3996
BLAKE2b-256 6638a4b59185a006b6c27f29ab3482ac2c5e0262616638be83fd3931f72f336d

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