#添加數(shù)據(jù)
user=User(username='username',password='123456')
db.session.add(user)
db.session.commit()
#讀取數(shù)據(jù)
users=User.query.all()
#返回一定的行數(shù)
users=User.query.limit(10).all()
#排序
users=User.query.order_by(User.username).all()
users=User.query.order_by(user.username.desc()).all()
#返回一行數(shù)據(jù)
users=User.query.first()
#通過主鍵獲取某一行數(shù)據(jù)
users=User.query.get(1)
#數(shù)據(jù)太多的話,可以分頁,第一個(gè)參數(shù)是第幾頁,第二個(gè)參數(shù)是一頁多少條數(shù)據(jù)
users=User.query.order_by('id').paginate(5,10)
#條件查詢 ? ? 查找所有姓名是username的數(shù)據(jù)
users=User.query.filter_by(username='username').all()
#條件查找 ? 按表達(dá)式查找 id<10的
users=User.query.filter(User.id<10).all()
#更新username=5的數(shù)據(jù) ,把用戶名改成test
User.query.filter_by(username='5').update({'username':'test'})
db.session.commit()
#刪除username=7的第一條數(shù)據(jù)
user=User.query.filter_by(username='7').first()
db.session.delete(user)
db.session.commit()