数据库操作工具包
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
Release history Release notifications | RSS feed
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.3.tar.gz
(20.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file database_smartools-0.0.3.tar.gz.
File metadata
- Download URL: database_smartools-0.0.3.tar.gz
- Upload date:
- Size: 20.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f4f05d3894aa985ae5b0146e370c278a3f05602c83fa752592bea2ff9c28970
|
|
| MD5 |
529642772cd5aee5edba9f4d4ee0b25c
|
|
| BLAKE2b-256 |
bac74d7b292efe76d5e5fcd297edea903a244b9340cec635e19f637d05adddfe
|
File details
Details for the file database_smartools-0.0.3-py3-none-any.whl.
File metadata
- Download URL: database_smartools-0.0.3-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec22f30264e5449d0134d999d335a30bd1250021d5e8621bbc9a47ad96de6f09
|
|
| MD5 |
1aacf5c3898ddcdc8760d7f19cd07c73
|
|
| BLAKE2b-256 |
3de7944d78c0c20688be068d88ab4166ab66aebc8d42b5ea33d31d15619f10e0
|