跨平台数据库连接管理工具
Project description
DB Connector - 跨平台数据库连接管理模块
一个安全、跨平台的Python数据库连接管理模块,支持主流数据库并提供加密存储功能。
特性
- 🔐 安全加密: 使用
cryptography.fernet加密敏感连接信息 - 📁 配置管理: 基于 TOML 的配置文件(读取:
tomllib,写入:tomli-w) - 🗄️ 多数据库支持:
- Oracle (oracledb)
- PostgreSQL (psycopg2/psycopg3)
- SQL Server (pymssql)
- MySQL (pymysql)
- SQLite (sqlite3)
- 🏗️ ORM 集成: 基于 SQLAlchemy 2.0+
- 📊 完整日志: 使用 logging 模块记录操作日志
- 🌐 跨平台: 支持 Windows、Linux、macOS
- 🧪 完整测试: 包含单元测试和集成测试
安装
要求
- Python >= 3.8
- 参见
requirements.txt了解依赖详情
从源码安装
git clone https://github.com/yourusername/db-connector.git
cd db-connector
pip install -e .
从 PyPI 安装(未来计划)
pip install db-connector
快速开始
基础用法
from db_connector import DatabaseManager
# 创建数据库管理器
db_manager = DatabaseManager()
# 添加数据库连接
mysql_config = {
'type': 'mysql',
'host': 'localhost',
'port': '3306',
'username': 'your_username',
'password': 'your_password',
'database': 'your_database'
}
db_manager.create_connection('my_mysql', mysql_config)
# 执行查询
results = db_manager.execute_query('my_mysql', 'SELECT * FROM users LIMIT 10')
print(results)
# 关闭连接
db_manager.close_all_connections()
多数据库操作
from db_connector import DatabaseManager
db_manager = DatabaseManager()
# 配置多个数据库
databases = {
'app_db': {
'type': 'postgresql',
'host': 'db.server.com',
'username': 'user',
'password': 'pass',
'database': 'application'
},
'log_db': {
'type': 'sqlite',
'database': '/path/to/logs.db'
}
}
for name, config in databases.items():
db_manager.create_connection(name, config)
# 跨数据库操作
users = db_manager.execute_query('app_db', 'SELECT * FROM users')
db_manager.execute_command('log_db', 'INSERT INTO access_log VALUES (?, ?)', (user_id, 'login'))
db_manager.close_all_connections()
配置说明
连接配置格式
每个数据库连接支持以下配置:
MySQL/PostgreSQL/SQL Server/Oracle:
{
'type': 'mysql', # mysql, postgresql, mssql, oracle
'host': 'localhost',
'port': '3306',
'username': 'your_username',
'password': 'your_password',
'database': 'your_database'
}
SQLite:
{
'type': 'sqlite',
'database': '/path/to/database.db' # 或 ':memory:' 用于内存数据库
}
配置文件位置
- 配置文件:
~/.config/db_connector/connections.toml - 日志文件:
~/.config/db_connector/logs/db_connector.log - 加密密钥:
~/.config/db_connector/encryption.key
API 参考
DatabaseManager
主要管理类,提供以下方法:
create_connection(name, config): 创建连接配置get_connection(name): 获取数据库连接execute_query(connection_name, query, params): 执行查询execute_command(connection_name, command, params): 执行命令test_connection(name): 测试连接list_connections(): 列出所有连接remove_connection(name): 删除连接close_connection(name): 关闭连接close_all_connections(): 关闭所有连接
开发
运行测试
# 运行所有测试
pytest
# 运行特定测试模块
pytest tests/test_database.py
# 带覆盖率的测试
pytest --cov=db_connector tests/
代码风格
本项目使用 Black 代码格式化工具:
black db_connector/ tests/ examples/
许可证
MIT License - 详见 LICENSE 文件
贡献
欢迎提交 Issue 和 Pull Request!
支持
如有问题请:
- 查看文档和示例
- 提交 GitHub Issue
- 联系维护者
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
db_connector_tool-0.0.4.tar.gz
(55.0 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 db_connector_tool-0.0.4.tar.gz.
File metadata
- Download URL: db_connector_tool-0.0.4.tar.gz
- Upload date:
- Size: 55.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d668e7d87703c0dea3cc4723f2937b639254127a55f72f1d67e78fba023d3500
|
|
| MD5 |
0ca3ea110d49beb4ca4008efb3f405b4
|
|
| BLAKE2b-256 |
8ea1346e3bf8db2dc0e7eb100ec94c2ebec1ec1a0e3fc65465f851fd85bb2171
|
File details
Details for the file db_connector_tool-0.0.4-py3-none-any.whl.
File metadata
- Download URL: db_connector_tool-0.0.4-py3-none-any.whl
- Upload date:
- Size: 55.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1b08477bb2bc2045f972694d9ca87d00273683a1207f9aff299e342c342cf1b
|
|
| MD5 |
7e70e4138e96697f01e88b0df805d821
|
|
| BLAKE2b-256 |
b890af341ae945772bb6a24d193caf248ad68b726471f37f600c7a10ee5365ef
|