圖解以下:
注意:增刪改都是經過db.session來操做的mysql
查的話是經過User.query.filter().all()sql
sqlachemy程序以及測試數據以下:
from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemyflask
app = Flask(__name__)
app.config['DEBUG']=Truesession
manage = Manager(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:mysql@127.0.0.1:3306/flask_test'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = Falseapp
db = SQLAlchemy(app)測試
class Role(db.Model):spa
__tablename__ = 'roles' id = db.Column(db.Integer,primary_key=True) name = db.Column(db.String(64),unique = True) us = db.relationship('User',backref='role') def __repr__(self): return 'Role %s'% self.name
class User(db.Model):debug
__tablename__ = 'users' id = db.Column(db.Integer,primary_key=True) name = db.Column(db.String(64),unique=True,index=True) email = db.Column(db.String(64),unique=True) password=db.Column(db.String(64)) role_id = db.Column(db.Integer,db.ForeignKey('roles.id')) def __repr__(self): return 'User:%s'%self.name
if name == '__main__':code
db.drop_all() db.create_all() # app.run(debug=True) # 建立測試數據 ro1 = Role(name='admin') db.session.add(ro1) db.session.commit() # 再次插入一條數據 ro2 = Role(name='user') db.session.add(ro2) db.session.commit() # 多條用戶數據 us1 = User(name='wang', email='wang@163.com', password='123456', role_id=ro1.id) us2 = User(name='zhang', email='zhang@189.com', password='201512', role_id=ro2.id) us3 = User(name='chen', email='chen@126.com', password='987654', role_id=ro2.id) us4 = User(name='zhou', email='zhou@163.com', password='456789', role_id=ro1.id) us5 = User(name='tang', email='tang@itheima.com', password='158104', role_id=ro2.id) us6 = User(name='wu', email='wu@gmail.com', password='5623514', role_id=ro2.id) us7 = User(name='qian', email='qian@gmail.com', password='1543567', role_id=ro1.id) us8 = User(name='liu', email='liu@itheima.com', password='867322', role_id=ro1.id) us9 = User(name='li', email='li@163.com', password='4526342', role_id=ro2.id) us10 = User(name='sun', email='sun@163.com', password='235523', role_id=ro2.id) db.session.add_all([us1, us2, us3, us4, us5, us6, us7, us8, us9, us10]) db.session.commit() manage.run(default_command='runserver')
測試指令以下:server
User.query.all()
User.query.count()
User.query.first()
User.query.get(4)
User.query.filter(User.id==4).all()
User.query.filter_by(id=4).all()
User.query.filter(User.name.endswith('g')).all()
User.query.filter(User.name.contains('g')).all()
User.query.filter(User.name!='wang').all()
from sqlalchemy import not_
User.query.filter(not_(User.name=='wang')).all()
from sqlalchemy import and_
User.query.filter(and_(User.name.startswith('li'),User.email.startswith('li')).all()
User.query.filter(User.name.startswith('li'),User.email.startswith('li')).all()
from sqlalchemy import or_
User.query.filter(or_(User.password=='123456',User.email.endswith('itheima.com'))).all()
User.query.filter(User.id.in_([1,3,5,7,9])).all()
Role.query.filter(Role.id==User.query.filter(User.name=='liu').first().role_id).first()
User.query.filter(User.name=='liu').first().role
Role.query.get(1).users
User.query.order_by('email').all()
User.query.order_by(User.email.desc()).all()
it = User.query.paginate(2,3,False)
it.pages -----總頁數
it.page -----當前頁
it.items -----當前頁的數據
------limit-----User.query.limit(3).all()