A lazy MySQL client for Python that simplifies database operations with intuitive methods for CRUD operations, automatic connection management, and result formatting. Features include easy-to-use SELECT, INSERT, UPDATE, DELETE operations with pandas DataFrame support, where clause builders, and table export capabilities.
Project description
Lazy_mysql
一个轻量级的Python库,为MySQL数据库操作提供简洁优雅的解决方案。
✨ 核心特性
- 🔌 统一SQL执行接口 - 简化复杂的数据库操作流程
- 📊 智能查询构建器 - 支持复杂条件、多表关联、排序限制
- 💾 批量数据操作 - 自动优化策略,支持超大数量级数据处理
- 🔄 Upsert支持 - 智能判断存在更新/不存在插入
- 🛡️ 安全防注入 - 参数化查询,自动SQL注入防护
- 📈 结果格式化 - 支持DataFrame、字典、列表等多种格式输出
- 📝 表结构导出 - 一键导出Markdown格式文档
- ⚡ 高性能优化 - LOAD DATA INFILE支持,百万级数据秒级处理
🚀 快速安装
pip install --upgrade lazy-mysql
🎯 快速开始
1. 数据库连接初始化
from lazy_mysql.executor import SQLExecutor
from lazy_mysql.sql_config import MySQLConfig
from lazy_mysql.tools.where_clause import NDayInterval
# 创建数据库配置
config = MySQLConfig(
host='localhost',
user='your_username',
passwd='your_password',
default_database='your_database'
)
# 创建执行器实例
executor = SQLExecutor(config)
2. 智能查询操作
# 基础查询
users = executor.select('users', ['id', 'name', 'email'])
print(users)
# 条件查询 + 排序限制
active_users = executor.select(
'users',
['id', 'name', 'email'],
conditions={'status': 'active', 'age': ('>', 18)},
order_by='created_at DESC',
limit=10
)
# 复杂条件查询
results = executor.select(
'users',
['id', 'name', 'score'],
conditions={
'status': ('IN', ['active', 'premium']),
'score': ('BETWEEN', [80, 100]),
'name': ('LIKE', '%John%'),
'order_dateTime': ('>=', NDayInterval(7)) # 最近7天
},
fetch_config={'output_format': 'df'} # 返回DataFrame格式
)
3. 批量数据插入
# 单条插入
executor.insert('users', {'name': '张三', 'email': 'zhang@example.com'}, commit=True)
# 批量插入(自动优化策略)
users_data = [
{'name': '李四', 'email': 'li@example.com', 'age': 25},
{'name': '王五', 'email': 'wang@example.com', 'age': 30},
# ... 支持数千条记录
]
inserted_count = executor.insert('users', users_data, commit=True)
# Upsert操作(存在更新,不存在插入)
user_data = {
'id': 1,
'name': '张三',
'email': 'new_email@example.com',
'updated_at': '2024-01-15 10:00:00'
}
executor.upsert('users', user_data, commit=True)
4. 数据更新与删除
# 条件更新
executor.update(
'users',
{'status': 'premium', 'updated_at': '2024-01-15 10:00:00'},
conditions={'last_login': ('>=', '2024-01-01'), 'points': ('>=', 1000)},
commit=True
)
# 安全删除(必须指定条件)
executor.delete('users', conditions={'status': 'inactive', 'last_login': ('<', '2023-01-01')}, commit=True)
5. 使用完毕后关闭连接
executor.close()
📚 详细文档
🔗 连接与配置
- 数据库连接初始化 - 连接配置、错误处理、重试机制、最佳实践
🔍 查询操作
- SELECT 查询操作 - 智能查询构建、复杂条件、多表关联、结果格式化
💾 数据修改
- INSERT 插入操作 - 批量插入、Upsert、大数据优化、重复处理
- UPDATE 更新操作 - 条件更新、批量更新、SQL表达式、性能优化
- DELETE 删除操作 - 安全删除、条件组合、错误处理、调试技巧
🔧 环境要求
- Python: 3.7+
- MySQL: 8.0.36+
- 依赖库:
mysql-connector-python>=9.4.0pandas>=2.3.1
⚠️ 兼容性说明: 低于上述版本要求的兼容性尚未验证
🎯 适用场景
- ✅ Web应用开发 - 快速原型开发、API后端服务
- ✅ 数据分析 - DataFrame集成、报表生成、数据清洗
- ✅ 批量数据处理 - 日志导入、数据迁移、ETL流程
- ✅ 企业级应用 - 事务处理、并发控制、错误重试
🏆 性能优势
| 操作类型 | 传统方式 | lazy_mysql优化 | 性能提升 |
|---|---|---|---|
| 批量插入1万条 | 3-5秒 | 0.5-1秒 | 5-10倍 |
| 批量插入10万条 | 30-60秒 | 2-5秒 | 15-30倍 |
| 复杂条件查询 | 手动拼接SQL | 智能构建 | 开发效率3倍 |
| 大数据导出 | 内存占用高 | 流式处理 | 内存节省80% |
📦 PyPI 项目
项目已发布到PyPI,可通过以下链接访问:
📄 开源协议
本项目采用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
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 lazy_mysql-0.4.0.tar.gz.
File metadata
- Download URL: lazy_mysql-0.4.0.tar.gz
- Upload date:
- Size: 27.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2b7c151c37076bd63a58e67bcf07b710ea0f425bc6481845c44945d94d2709a
|
|
| MD5 |
72a2ac4cb239ec5cf900b40f503c77f5
|
|
| BLAKE2b-256 |
92f9476734f50cfc10897b8c43e40f446b656d4ec23672224440b00ec55ff4c0
|
File details
Details for the file lazy_mysql-0.4.0-py3-none-any.whl.
File metadata
- Download URL: lazy_mysql-0.4.0-py3-none-any.whl
- Upload date:
- Size: 31.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9930f00d7858b921aa6af9963327c7edc803e3ba93b8229205243c1fcc9c54c4
|
|
| MD5 |
4aec7cb433825142b3a486ca8419d802
|
|
| BLAKE2b-256 |
5a4c392c052d7c15d6b61a853ffda51aaa76b44b9dfd19ac3116cca0850d96f7
|