from flask import Blueprint,render_template,request
from App.extensions import db
from App.models import User
import random
test = Blueprint(‘test’,name)
@test.route(‘/create_table/’)
def create_table():
db.drop_all()
db.create_all()
return ‘create_table’
@test.route(‘/insert_one/’)
def insert_one():
try:
# u = User(username=’zhangshang’,sex=False,age=18)
u = User(‘lisi’,True,20)
db.session.add(u)
db.session.commit()
except:
db.session.rollback()
return ‘insert_one’
@test.route(‘/auto_insert_one/’)
def auto_insert_one():
u = User(‘王五’,False,30)
db.session.add(u)
return ‘insert_one’
@test.route(‘/self_insert_one/’)
def self_insert_one():
u = User(‘兆六’,False,20)
# db.session.add(u)
u.save()
return ‘insert_one’
@test.route(‘/self_insert_many/’)
def self_insert_many():
sex = [True,False]
u1 = User(random.randrange(1000,10000),sex[random.randint(0,1)],random.randint(1,100))
u2 = User(random.randrange(1000,10000),sex[random.randint(0,1)],random.randint(1,100))
# db.session.add_all([u1,u2])
# db.session.commit()
User.save_all(u1,u2)
return ‘添加多条’
@test.route(‘/delete/’)
def delete():
u = User.query.get(2)
# u.username = ‘张三’
# db.session.delete(u)
# db.session.commit()
u.delete()
return ‘删除’
@test.route(‘/update/’)
def update():
u = User.query.get(2)
u.username = ‘张三’
# db.session.delete(u)
# db.session.commit()
u.save()
return ‘修改’
@test.route(‘/all/’)
def show():
# dataList = User.query.all()[1:4]
dataList = User.query.all()[1:4]
print(dataList)
return render_template(‘show.html’,data=dataList)
@test.route(‘/filter/’)
def filter():
# data = User.query.filter() #查询所有
# data = User.query.filter(User.sex==True) #查询所有
data = User.query.filter(User.sex==True,User.age==43) #查询所有
# data = User.query.filter(User.sex!=True) #查询所有
# data = User.query.filter(User.sex!=True,User.age>=30) #查询所有
# data = User.query.filter(User.age.__gt__(30)) #查询所有
# data = User.query.filter(User.age.__ge__(30)) #查询所有
# data = User.query.filter(User.age.__lt__(30)) #查询所有
# data = User.query.filter(User.age.__le__(30)) #查询所有
# data = User.query.filter(User.age.__gt__(30),User.age.__lt__(70)) #查询所有
print(data)
return render_template('show.html',data=data)
@test.route(‘/filter_by/’)
def filter_by():
data = User.query.filter_by() #查询所有
data = User.query.filter_by(sex=True) #查询sex为True的数据
data = User.query.filter_by(sex=True,age=43) #查询sex为True 并且 age为43的数据
print(data)
return render_template(‘show.html’, data=data)
@test.route(‘/offset//’)
def offset(num):
data = User.query.offset(num)
return render_template(‘show.html’, data=data)
@test.route(‘/limit//’)
def limit(num):
data = User.query.limit(num)
return render_template(‘show.html’, data=data)
@test.route(‘/offsetlimit//’)
def offsetlimit(num):
data = User.query.offset(num).limit(num)
return render_template(‘show.html’, data=data)
@test.route(‘/order_by/’)
def order_by():
# data = User.query.order_by(User.id)
# data = User.query.order_by(-User.id)
# data = User.query.filter_by(sex=True).order_by(-User.id)
data = User.query.order_by(-User.age).limit(1)
return render_template(‘show.html’, data=data)
@test.route(‘/first/’)
def first():
data = User.query.order_by(-User.id).first()
print(data)
return ‘first’
# return render_template(‘show.html’, data=data)
@test.route(‘/get/’)
def myGet():
data = User.query.get(20)
# print(data.username)
print(data)
return ‘get’
@test.route(‘/contains/’)
def contains():
data = User.query.filter(User.username.contains(‘1’))
return render_template(‘show.html’, data=data)
@test.route(‘/like/’)
def like():
# data = User.query.filter(User.username.like(‘%1%’))
# data = User.query.filter(User.username.like(‘%1’))
data = User.query.filter(User.username.like(‘1%’))
return render_template(‘show.html’, data=data)
@test.route(‘/startend/’)
def startend():
# data = User.query.filter(User.username.startswith(‘张’))
data = User.query.filter(User.username.endswith(‘六’))
return render_template(‘show.html’, data=data)
@test.route(‘/in/’)
def myIn():
# data = User.query.filter(User.id.in_([1,2,3,4]))
data = User.query.filter(~User.id.in_([1,2,3,4]))
return render_template(‘show.html’, data=data)
@test.route(‘/null/’)
def null():
data = User.query.filter(User.username.is_(None))
data = User.query.filter(User.username == None)
data = User.query.filter(~User.username.isnot(None))
# data = User.query.filter(User.username!=None)
# data = User.query.filter(~User.username.is_(None))
# data = User.query.filter(User.username.isnot(None))
return render_template('show.html', data=data)
from sqlalchemy import and_,or_,not_
@test.route(‘/and/’)
def myAnd():
# data = User.query.filter(User.username.contains(‘1’),User.sex==True)
# data = User.query.filter(and_(User.username.contains(‘1’),User.sex==True))
data = User.query.filter(User.username.contains(‘1’)).filter(User.sex==True)
return render_template('show.html', data=data)
@test.route(‘/or/’)
def myOr():
data = User.query.filter(or_(User.username.contains(‘1’)))
data = User.query.filter(or_(User.username.contains(‘1’),User.sex==True),User.age>=60)
return render_template(‘show.html’, data=data)
@test.route(‘/not/’)
def myNot():
# data = User.query.filter(not_(User.username.contains(‘1’)))
# data = User.query.filter(~(User.username.contains(‘1’),User.sex==True))
data = User.query.filter(~(User.username.contains(‘1’)))
return render_template(‘show.html’, data=data)
@test.route(‘/count/’)
def count():
data = User.query.filter().count()
print(data)
return ‘{}’.format(data)