Esempio n. 1
0
from crud import db_session
from create_table import User

# user_list = db_session.query(User).all()
#
# for user in user_list:
#     print(user.name)

# user = db_session.query(User).first()
# print(user.name)

# 条件查询
# user_list = db_session.query(User).filter(User.id == 3).all()
# print(user_list[0].id, user_list[0].name)

# user_list = db_session.query(User).filter(User.id > 3).all()
# print(user_list[0].id, user_list[0].name)

user_list = db_session.query(User).filter_by(id=3).all()
print(user_list[0].id, user_list[0].name)

Esempio n. 2
0
from crud import db_session

from create_table_foreignkey import Student, School

# 删除数据
sch = db_session.query(School).filter(School.name == 'xdf').first()
stu = db_session.query(Student).filter(Student.school_id == sch.id).delete()
db_session.commit()
db_session.close()
Esempio n. 3
0
from crud import db_session

from create_table_foreignkey import Student, School

# relationship 正向
# stu_list = db_session.query(Student).all()
#
# for stu in stu_list:
#     print(stu.id, stu.name, stu.school_id, stu.stu2sch.name)

# relationship 反向
sch_list = db_session.query(School).all()

for sch in sch_list:
    for student in sch.sch2stu:
        print(student.name)
Esempio n. 4
0
from crud import db_session
from create_table import User

db_session.query(User).filter_by(id=3).update({'name': ''})

#在原有值基础上添加 - 1
db_session.query(User).filter(User.id > 0).update({User.name: User.name + '007' }, synchronize_session=False)

#在原有值基础上添加 - 2
# db_session.query(User).filter(User.id > 0).update({"age": User.age + 1}, synchronize_session="evaluate")
db_session.commit()
db_session.close()
Esempio n. 5
0
from crud import db_session

from create_table_foreignkey import Student, School

# 修改数据
sch = db_session.query(School).filter(School.name == 'xdf').first()
stu = db_session.query(Student).filter(Student.name == 'lhc').update(
    {'school_id': sch.id})
db_session.commit()
db_session.close()