0%
增
1 2 3 4 5 6 7
| student = Student(id=id, name=name, age=age) try: db.session.add(student) db.session.commit() except: db.session.rollback()
|
删
1 2
| db.session.delete(user) db.session.commit()
|
查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| user_list = Wdtest.query.all()
user_list_num = Wdtest.query.count()
user= Wdtest.query.first()
user = Wdtest.query.get(3) user_list = Wdtest.query.filter_by(id=3).all() user_list = Wdtest.query.filter(Wdtest.id == 3).all()
Wdtest.query.filter(Wdtest.name.endswith('g')).all()
user_list = Wdtest.query.filter(Wdtest.name.contains('wa')).all()
user_list = Wdtest.query.filter(Wdtest.name.like('%a%')).all()
user_list = Wdtest.query.filter(Wdtest.name.startswith('wa'), Wdtest.age == 20).all() from sqlalchemy import and_ user_list = Wdtest.query.filter(and_(Wdtest.name.startswith('wa'), Wdtest.age == 20)).all()
from sqlalchemy import or_ user_list = Wdtest.query.filter(or_(Wdtest.name.startswith('wa'), Wdtest.age == 20)).all()
from sqlalchemy import not_ user_list = Wdtest.query.filter(not_(Wdtest.name == 'wade')).all() user_list = Wdtest.query.filter(Wdtest.name != 'wade').all()
user_list = Wdtest.query.filter(Wdtest.id.in_(['97124f50-0208-11ea-a66c-04ea56212bdf', '3'])).all()
user_list = Wdtest.query.order_by(Wdtest.age, Wdtest.id.desc()).limit(5).all()
pn = Wdtest.query.paginate(2,3) print(pn.pages) print(pn.page) print(pn.items)
|
改
1 2 3 4 5 6 7
| user = User.query.first() user.name = 'silence' db.session.commit() User.query.first()
User.query.filter_by(name='zhang').update({'name':'silence'})
|