天下程序员首页cx06.com
管理 |  登录 |  注册
袁鹏飞的小屋
笔记(共39个) > python

python orm工具链式操作mysql数据库Chain-PyMySQL

阅读 0赞 0回复 2024-07-19 10:55:09
导航目录:共(7)个目录

一个基于 PyMySQL 重新封装的 MySql 操作库,增加链式操作,方便快捷进行 CURD 操作

PyMySql 是使用 python 来操作 MySql 的常用库,但 pymysql 只是实现了基础功能,CURD 都需求自己拼接 sql 来执行,相当不方便。 PyMySql 官方文档:官方文档地址

git上地址https://github.com/Tiacx/chain-pymysql


温馨提示:不好用,有bug,好不容易都准备好,真是备受打击!可能我版本不太对,大家自己测试吧

吐槽:果然python连接数据库这块没有php舒服,也可能我还不太熟悉。


1、安装和连接

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'
})

数据库连接相关:(可以以后看)


2、切换数据库连接

支持连接多个数据库,使用 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

3、功能介绍

增加、修改、删除数据

#增加一条
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': '郑十'},
])

赞(0)

文章作者置顶的回复

全部回复列表 当前第(1)页

添加回复,文明发言,会审核.(服务区回复可以发广告)

作者最新笔记
天下程序员 www.cx06.com 程序员的网上家园!
作者微信:13126507001