A well-encapsulated Python class tool for MySQL database operations
Project description
DB MySQL Tool
A well-encapsulated Python class tool for MySQL database operations, providing easy-to-use CRUD operations and connection management.
Features
- 🚀 Simple and intuitive API for MySQL operations
- 🔄 Automatic connection management with context manager support
- 📊 Support for both single and bulk operations
- 🛡️ Transaction support with automatic rollback on errors
- 📝 Comprehensive logging
- 🎯 Type hints for better development experience
Installation
pip install db-mysql-tool
Quick Start
from db_mysql_tool import MySQLUtils
# 初始化数据库连接
db_config = {
'host': 'localhost',
'user': 'your_username',
'password': 'your_password',
'database': 'your_database',
'port': 3306
}
# 使用上下文管理器自动管理连接
with MySQLUtils(**db_config) as db:
# 创建表
create_table_sql = """
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
age INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
db.execute_update(create_table_sql)
# 插入数据
user_data = {'name': 'John Doe', 'email': 'john@example.com', 'age': 30}
user_id = db.insert('users', user_data)
print(f"Inserted user with ID: {user_id}")
# 查询数据
users = db.select('users', condition='age > %s', params=(25,))
for user in users:
print(user)
# 连接会自动关闭
Advanced Usage
Bulk Operations
with MySQLUtils(**db_config) as db:
users_data = [
{'name': 'Alice', 'email': 'alice@example.com', 'age': 25},
{'name': 'Bob', 'email': 'bob@example.com', 'age': 30},
{'name': 'Charlie', 'email': 'charlie@example.com', 'age': 35}
]
affected_rows = db.insert_many('users', users_data)
print(f"Inserted {affected_rows} users")
Complex Queries
with MySQLUtils(**db_config) as db:
# 复杂查询
users = db.select(
table='users',
columns='name, email, age',
condition='age BETWEEN %s AND %s AND name LIKE %s',
params=(20, 40, '%A%'),
order_by='age DESC',
limit=10
)
API Documentation
MySQLUtils Class
Initialization
MySQLUtils(host, user, password, database, port=3306, charset='utf8mb4')
Main Methods
connect(): 连接到数据库disconnect(): 断开数据库连接execute_query(sql, params): 执行查询语句execute_update(sql, params): 执行增删改操作insert(table, data): 插入单条数据insert_many(table, data_list): 批量插入数据update(table, data, condition, condition_params): 更新数据delete(table, condition, params): 删除数据select(table, columns, condition, params, order_by, limit): 查询数据select_one(table, columns, condition, params): 查询单条数据
Testing
# 安装开发依赖
pip install db-mysql-tool[dev]
# 运行测试
pytest tests/
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
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_mysql_tool-0.1.1.tar.gz.
File metadata
- Download URL: db_mysql_tool-0.1.1.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb2c983788907059ba3d42c2a8af35fdb66b37a82f77950854a1539ead583c8a
|
|
| MD5 |
046085257730da27e6f5ac96b3cf6e86
|
|
| BLAKE2b-256 |
d0133ebc67e7600e16c05aa74ae3263fa6e65d49acd8a778a7d1549af8ef11e4
|
File details
Details for the file db_mysql_tool-0.1.1-py3-none-any.whl.
File metadata
- Download URL: db_mysql_tool-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38e5dda028c97dcb9fe6739e6290787fbb0492b2898771b865db499fc7d31462
|
|
| MD5 |
38a2bd45b827a7b23f0149975bd4b805
|
|
| BLAKE2b-256 |
da433f1c831b1e84dc9ed220281b49e16f5b95043331371059f0fdd9a7df4473
|