sqlalchemy 单表增删改查

时间:2022-02-22 04:49:09

1、连接数据库,并创建session

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

engine = create_engine(
        "mysql pymysql://root:密码@127.0.0.1:3306/数据库?charset=utf8",
        max_overflow=0,  # 超过连接池大小外最多创建的连接
        pool_size=5,  # 连接池大小
        pool_timeout=30,  # 池中没有线程最多等待的时间,否则报错
        pool_recycle=-1  # 多久之后对线程池中的线程进行一次连接的回收(重置)
    )

SessionFactory = sessionmaker(bind=engine)

session = SessionFactory()

2、增

# 单条
obj = Users(name=tom)
session.add(obj)
session.commit()
# 多条
session.add_all([
        Users(name=海贼王),
        Users(name=死神)
])
session.commit()
session.close()

3、删

session.query(Users).filter(Users.id >= 2).delete()
session.commit()
session.close()

4、改

session.query(Users).filter(Users.id == 4).update({Users.name:死神})
session.query(Users).filter(Users.id == 4).update({name:火影})
# 在原本的字段,修改属性
session.query(Users).filter(Users.id == 4).update({name:Users.name "DSB"},synchronize_session=False)
session.commit()
session.close()

5、查

# 查询所有
result = session.query(Users).all()
for row in result:
        print(row.id,row.name)
# 根据条件查询
result = session.query(Users).filter(Users.id >= 2)
for row in result:
        print(row.id,row.name)
# 查询id大于2的第一个对象
result = session.query(Users).filter(Users.id >= 2).first()
print(result)

补充

# 1. 指定列, 字段别名
# select id,name as cname from users;
result = session.query(Users.id,Users.name.label(cname)).all()
for item in result:
        print(item[0],item.id,item.cname)
# 2. 默认条件and
session.query(Users).filter(Users.id > 1, Users.name == abc).all()
# 3. between
session.query(Users).filter(Users.id.between(1, 3), Users.name == abc).all()
# 4. in, not in ~
session.query(Users).filter(Users.id.in_([1,3,4])).all()
session.query(Users).filter(~Users.id.in_([1,3,4])).all()
# 5. 子查询
session.query(Users).filter(Users.id.in_(session.query(Users.id).filter(Users.name==abc))).all()
# 6. and 和 or
from sqlalchemy import and_, or_
# 默认 and
session.query(Users).filter(Users.id > 3, Users.name == eric).all()
# and
session.query(Users).filter(and_(Users.id > 3, Users.name == eric)).all()
# or
session.query(Users).filter(or_(Users.id < 2, Users.name == eric)).all()
# and or 一起使用
session.query(Users).filter(
    or_(
        Users.id < 2,
        and_(Users.name == eric, Users.id > 3),
        Users.extra != ""
    )).all()

# 7. filter_by,查询内部执行的是filter
session.query(Users).filter_by(name=abc).all()

# 8. 通配符 % 任意个, _一个
ret = session.query(Users).filter(Users.name.like(a_)).all()
ret = session.query(Users).filter(~Users.name.like(e%)).all()

# 9. 切片/分页
result = session.query(Users)[1:2]

# 10.排序
ret = session.query(Users).order_by(Users.name.desc()).all()
ret = session.query(Users).order_by(Users.name.desc(), Users.id.asc()).all()

# 11. group by, having , 聚合函数
from sqlalchemy.sql import func

ret = session.query(
        Users.depart_id,
        func.count(Users.id),
).group_by(Users.depart_id).all()
for item in ret:
        print(item)

# having
from sqlalchemy.sql import func

ret = session.query(
        Users.depart_id,
        func.count(Users.id),
).group_by(Users.depart_id).having(func.count(Users.id) >= 2).all()
for item in ret:
        print(item)

# 12.union 和 union all, unuon 去重 union all 不去重
"""
select id,name from users
UNION
select id,name from users;
"""
# 去重
q1 = session.query(Users.name).filter(Users.id > 2)
q2 = session.query(Favor.caption).filter(Favor.nid < 2)
ret = q1.union(q2).all()
# 不去重
q1 = session.query(Users.name).filter(Users.id > 2)
q2 = session.query(Favor.caption).filter(Favor.nid < 2)
ret = q1.union_all(q2).all()

注意:

1、操作数据结束,关闭session

session.close()

2、增、删、改,要提交数据

session.commit()