跨平台数据库连接管理工具 - 支持多种数据库的安全连接管理
Project description
DB Connector
一个安全、跨平台的Python数据库连接管理工具,支持多种主流数据库并提供加密存储功能。
✨ 特性
- 🔐 安全加密: 使用
cryptography.fernet加密敏感连接信息 - 📁 配置管理: 基于 TOML 的配置文件,支持备份功能
- 🗄️ 多数据库支持:
- Oracle (oracledb)
- PostgreSQL (psycopg3)
- SQL Server (pymssql)
- MySQL (pymysql)
- SQLite (sqlite3)
- 🏗️ ORM 集成: 基于 SQLAlchemy 2.0+ 的现代化ORM支持
- 📊 完整日志: 使用 logging 模块记录详细操作日志
- 🌐 跨平台: 支持 Windows、Linux、macOS
- 🧪 完整测试: 包含单元测试和集成测试
- 💻 CLI 工具: 提供命令行界面进行连接管理
📦 安装
从源码安装
git clone https://github.com/wangquanqing/db-connector-tool.git
cd db-connector-tool
pip install -e .
从 PyPI 安装
pip install db-connector-tool
🚀 快速开始
基础用法
from db_connector import DatabaseManager
# 创建数据库管理器
db_manager = DatabaseManager()
# 添加MySQL连接配置
mysql_config = {
'type': 'mysql',
'host': 'localhost',
'port': 3306,
'username': 'your_username',
'password': 'your_password',
'database': 'your_database'
}
db_manager.add_connection('mysql_db', mysql_config)
# 执行查询
results = db_manager.execute_query('mysql_db', '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.add_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()
🔧 命令行工具
基本命令
# 查看帮助
db-connector --help
# 列出所有连接
db-connector list
# 添加新连接
db-connector add mysql-dev --type mysql --host localhost --username root --password your_password --database app_db
# 测试连接
db-connector test mysql-dev
# 执行查询
db-connector query mysql-dev "SELECT * FROM users"
# 进入交互式SQL Shell
db-connector shell mysql-dev
📋 API 参考
DatabaseManager 类
主要管理类,提供以下方法:
add_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()- 关闭所有连接
ConfigManager 类
配置管理类,提供以下方法:
get_connection(name)- 获取连接配置remove_connection(name)- 删除连接配置list_connections()- 列出所有连接配置
🔒 安全特性
加密存储
所有敏感信息(密码、连接字符串等)都会自动加密存储:
# 配置会自动加密存储
config = {
'type': 'mysql',
'host': 'localhost',
'username': 'user',
'password': 'secret_password'
}
配置文件位置
- 配置文件:
~/.config/db_connector/connections.toml - 日志文件:
~/.config/db_connector/logs/db_connector.log - 加密密钥:
~/.config/db_connector/encryption.key
🧪 开发
运行测试
# 运行所有测试
pytest
# 运行特定测试模块
pytest tests/test_database.py
# 带覆盖率的测试
pytest --cov=db_connector tests/
# 运行快速测试(跳过慢速测试)
pytest -m "not slow"
代码质量
# 代码格式化
black db_connector/ tests/
# 代码检查
flake8 db_connector/ tests/
# 类型检查
mypy db_connector/
📄 许可证
本项目采用 MIT 许可证 - 详见 LICENSE 文件。
🤝 贡献
欢迎提交 Issue 和 Pull Request!
- Fork 本项目
- 创建功能分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 打开 Pull Request
📞 支持
如有问题请:
- 查看文档和示例代码
- 提交 GitHub Issue
- 联系维护者: wangquanqing1636@sina.com
📚 相关项目
- SQLAlchemy - Python SQL 工具包和 ORM
- cryptography - Python 加密库
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.5.tar.gz
(56.7 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.5.tar.gz.
File metadata
- Download URL: db_connector_tool-0.0.5.tar.gz
- Upload date:
- Size: 56.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
234b10ae3ae148ea4a65d5d37cef18ce7f5f6102e9984f110886e60ae0293704
|
|
| MD5 |
813789d821bfbfa1007ca48287a4b7f2
|
|
| BLAKE2b-256 |
ef293055bcf3888e1ac6701a89573354ac3db8069e2098ea6b521ddf5744bbdc
|
File details
Details for the file db_connector_tool-0.0.5-py3-none-any.whl.
File metadata
- Download URL: db_connector_tool-0.0.5-py3-none-any.whl
- Upload date:
- Size: 56.3 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 |
6848665269ae17de93666456b818340520889defb01f901ec700bf853685a2e6
|
|
| MD5 |
4f1dca38e4e829c548e2e930c1f92afd
|
|
| BLAKE2b-256 |
35b4383f2a4163659b4aec4da898caff9200221bb8268cd354825d4ce431a0cd
|