一个基于 PyMySQL 重新封装的 MySql 操作库,增加链式操作,方便快捷进行 CURD 操作
PyMySql 是使用 python 来操作 MySql 的常用库,但 pymysql 只是实现了基础功能,CURD 都需求自己拼接 sql 来执行,相当不方便。 PyMySql 官方文档:官方文档地址
git上地址https://github.com/Tiacx/chain-pymysql
温馨提示:不好用,有bug,好不容易都准备好,真是备受打击!可能我版本不太对,大家自己测试吧
吐槽:果然python连接数据库这块没有php舒服,也可能我还不太熟悉。
Method: imysql.connect(options: dict, name='default')
pip install chain-pymysql from chain_pymysql import imysql imysql.connect({ 'host': '127.0.0.1', 'user': 'root', 'password': 'root', 'database': 'test' })
数据库连接相关:(可以以后看)
支持连接多个数据库,使用 switch 方法来动态(或永久)切换数据库连接
Method: imysql.switch(name: str, db_name=None, inplace=False)
switch 方法使用示例
imysql.switch('db2')
imysql.switch('db2', inplace=True)
imysql.switch('db2', 'test_member')
或 imysql.switch('db2.test_member')
from chain_pymysql import imysql imysql.connect(default_config, name='default') # 第一个添加的数据库为默认数据库 imysql.connect(other_config, name='other') # 使用默认数据库连接查询 imysql.table('table1').limit(10).all() # 临时使用其他数据库连接来查询(只有本次查询是使用db2,其他查询还是db1) imysql.switch('other').table('table1').limit(10).all() # 永久切换数据库连接 imysql.switch('other', inplace=True).table('table1').limit(10).all() # 这时再查询,默认就是 db2 数据库连接了 imysql.table('table1').limit(10).all() # 这里查询的是 db2 连接里的 table1
#增加一条 insert_id = imysql.table('table1').insert_one({'id': 1, 'name': '张三'}) #增加多条 effected_rows = imysql.table('table1').insert_many([ {'id': 2, 'name': '李四'}, {'id': 3, 'name': '王五'}, {'id': 4, 'name': '赵六'} ]) #删除一条 effected_rows = imysql.table('table1').delete({'id': 1}) # 限制删除的行数 effected_rows = imysql.table('table1').delete('id>1', limit=1) #更新一条 effected_rows = imysql.table('table1').update_one({'id': 3}, {'name': '王六'}) #更新多条 effected_rows = imysql.table('table1').update_many('id IN (3,4)', {'name': '匿名'})
fetch=True 返回 list,fetch=False(默认)返回 cursor,可用于迭代
多条件列表查询
# 普通查询 results = imysql.table('table1').where({'id': 3}).all(fetch=True) # IN 查询 results = imysql.table('table1').where({'id': ['in', (3, 4)]}).all(fetch=True) # LIKE 模糊查询 results = imysql.table('table1').where({'name': ('like', '张%')}).all(fetch=True) # BETWEEN 查询 results = imysql.table('table1').where({'id': ['between', (3, 4)]}).all(fetch=True) # 大于等于 results = imysql.table('table1').where({'id': ['>=', 3]}).all(fetch=True) # 小于 results = imysql.table('table1').where({'id': ['<', 4]}).all(fetch=True) # 不等于 results = imysql.table('table1').where({'id': ['<>', 3]}).all(fetch=True) # 不为空 results = imysql.table('table1').where({'id': ['<>', '']}).all(fetch=True) # NULL results = imysql.table('table1').where({'name': ['is', None]}).all(fetch=True) # NOT NULL results = imysql.table('table1').where({'name': ['is not', None]}).all(fetch=True) # NOT IN results = imysql.table('table1').where({'id': ['not in', (3, 4)]}).all(fetch=True) # NOT LIKE results = imysql.table('table1').where({'name': ['not like', '张%']}).all(fetch=True)
单列查询
one = imysql.table('table1').select('id').where('id=3').one()
分组、排序、多条件查询
results = ( imysql.table('table2') .select('age, count(*) as num') # 可以使用字符串或list .group_by('age') # .group_by(['age']) # 可以使用字符串或list # .order_by('age asc, num desc') # .order_by(['age asc', 'num desc']) # .order_by(['age', 'num'], ascending=True) .order_by(['age', 'num'], ascending=[True, False]) .all(fetch=True) )
分页查询与原生查询
results = imysql.table('table1').order_by('id asc').skip(1).limit(3).all(fetch=True) sql = 'SELECT * FROM table1 WHERE id=%s' # 解析多行 results = imysql.execute(sql, (3,)).all(fetch=True) # 解析单行 one = imysql.execute(sql, (3,)).one() # 其他操作(详情请看 github) names = imysql.execute(sql, (3,)).index('id', 'name')
获取sql、id和影响行
# 方法一:执行前获取 sql = imysql.table('table1').select('id,name').limit(2).get_raw_sql() # 方法二:执行后获取 sql = imysql.get_last_sql() # 方法一:insert_one 方法返回值 获取id id1 = imysql.table('table1').insert_one({'name': '周八'}) # 方法一:函数返回值 影响行 result = imysql.table('table1').insert_many([ {'name': '吴九'}, {'name': '郑十'}, ])