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 import SQLExecutor, MySQLConfig, NDayInterval
# 创建数据库配置
config = MySQLConfig(
host='localhost',
user='your_username',
passwd='your_password',
database='your_database'
)
# 指定数据库
executor = SQLExecutor(config, database='database')
# 不传入配置时自动从环境变量读取:
# LAZY_MYSQL_HOST / LAZY_MYSQL_PORT / LAZY_MYSQL_USER / LAZY_MYSQL_PASSWD / LAZY_MYSQL_DATABASE
# 支持混合配置:host/user/passwd 从环境变量读取,database 由参数指定
executor = SQLExecutor(database='another_db')
2. 智能查询操作
# 基础查询(select 自动构造 SQL)
users = executor.select('users', ['id', 'name', 'email'])
print(users)
# 手写复杂 SQL(query 直接执行)
result = executor.query(
"SELECT id, name, RANK() OVER (ORDER BY score DESC) as rank FROM users",
fetch_config={'output_format': 'df_dict', 'data_label': ['id', 'name', 'rank']}
)
# 条件查询 + 排序限制
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.close()
# 提交数据并关闭连接
executor.commit_close()
📚 详细文档
🔗 连接与配置
- 数据库连接初始化 - 连接配置、错误处理、重试机制、最佳实践
- WHERE 条件构造 - 等值条件、比较运算符、空值判断、日期筛选
🔍 查询操作
- SELECT 查询操作 - 智能查询构建、复杂条件、多表关联、结果格式化
- 自定义 SQL 查询 - 手写 SQL 执行、子查询、UNION、窗口函数
- FetchConfig 配置 - fetch_mode、output_format、data_label 等结果格式化参数
💾 数据修改
- INSERT 插入操作 - 批量插入、大数据优化、重复处理
- UPSERT 插入或更新 - 存在则更新、不存在则插入
- UPDATE 更新操作 - 条件更新、批量更新、SQL表达式、性能优化
- DELETE 删除操作 - 安全删除、条件组合、错误处理、调试技巧
🛠️ SQL工具函数
- SQL工具函数 - add_limit条件构建、build_where/build_sql_with_where WHERE子句构建、resolve_sql智能路径解析、load_sql文件加载
📦 PyPI 项目
项目已发布到PyPI,可通过以下链接访问:
🔧 环境要求
- Python: 3.10+
- MySQL: 8.0.36+
- 依赖库:
mysql-connector-python>=9.4.0pandas>=2.3.1
📄 开源协议
本项目采用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.6.3.tar.gz.
File metadata
- Download URL: lazy_mysql-0.6.3.tar.gz
- Upload date:
- Size: 40.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
788a3f6d29628756f5d8c64336941a8db7d30e833264d3b174bb48aaf1679270
|
|
| MD5 |
069aeb11d9b4c22db85a0f593aad3495
|
|
| BLAKE2b-256 |
cb7ed13c1a91eb21d3d94d9810b2a484cae4ce2bfd76d7e7bc6a8dc19cc91618
|
File details
Details for the file lazy_mysql-0.6.3-py3-none-any.whl.
File metadata
- Download URL: lazy_mysql-0.6.3-py3-none-any.whl
- Upload date:
- Size: 41.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df4738b559a5e4957e3f7d206f7c571610f1665f984bce3292176a9f89eace17
|
|
| MD5 |
bc72bfd80fe61b4f2b056ec10fdfbc5e
|
|
| BLAKE2b-256 |
f88d7d13704a42c031bc498767729be7a7f7f30eeb64d331d28c41613c4a9ffb
|