SQLALchemy数据库
SQLALchemy是一个关系型数据库框架,它提供了高层的ORM和底层的原生数据库的操作。 flask-sqlalchemy是一个简化了SQLALchemy操作的flask扩展。
flask使用SQLALchemy操作数据库
在flask的虚拟环境中先安装:flask_sqlalchemy ,pymysql
pip install flask_sqlalchemy
pip install pymysql
在init.py中配置数据库
import os
from flask import Flask
from flask_sqlalchemy import SQLALchemy
from Stu.views import stu
def create_app():
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
template_dir = os.path.join(BASE_DIR,'templates')
static_dir = os.path.join(BASE_DIR,'static')
app=Flask(__name__,template_floder=template_dir,static_floder=static_dir)
# 配置数据库
app.config['SQLALCHEMY_DATABASE_URI']= 'mysql+pymysql://root:123456@localhost:3306/flask3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = Falseie
# 注册蓝图
app.register_blueprint(blueprint=stu,url_prefix='/stu')
#初始化app
SQLALchemy(app=app)
return app
在model.py 创建模型
from flask_sqlalchemy import SQLALchemy
db = SQLALchemy()
class Student(db.model):
s_id = db.Column(db.Integer,primary_key=True,autoincrement=True)
s_name = db.Column(db.String(20),unique =True)
s_age = db.Column(db.Integer,default=18)
__tablename__ = 'student'
常用的字段类型:flask常用字段类型
创建表和增删改查
在views.py中:
import random
from flask import Blueprint, render_template, request, redirect, url_for, make_response
from Stu.models import db, Student
stu = Blueprint('stu', __name__)
@stu.route('/')
def index():
return render_template('index.html')
@stu.route('/score/')
def score():
score_list = [21, 34, 33, 45, 67, 78]
content_h2 = '<h2>少男<h2>'
content_h3 = ' <h3>速度快解散<h3>'
return render_template('score.html', score=score_list,
content_h2=content_h2,
content_h3=content_h3)
# 创建表
@stu.route('/createtable/')
def create_db():
db.create_all()
return '创建成功'
# 删除表
@stu.route('/droptable/')
def drop_db():
db.drop_all()
return '删除成功'
# 在数据库创建单个学生
@stu.route('/createstu/')
def create_stu():
stu = Student()
stu.s_name = '小帅%d' % random.randrange(1000)
stu.s_age = '%d' % random.randrange(20)
db.session.add(stu)
try:
db.session.commit()
except:
db.session.rollback()
return '创建学生成功'
# 一次创建多个学生 关键字:db.session.add_all(列表)
@stu.route('/createmoneystu/')
def create_money_stu():
stu_list = []
stu1 = Student(username1,age1)
stu2 = Student(username2,age2)
stu_list.append(stu1)
stu_list.append(stu2)
db.session.add_all(stu_list)
db.session.commit
return '创建多个学生成功'
# 查询所有方法
@stu.route('/stulist/')
def stu_all():
# 第一种查询所有
stus = Student.query.all()
return render_template('studentlist.html', stus=stus)
# 查询一个学生方法
@stu.route('/studentail/')
def stu_detail():
# 原生的SQL语句查询
# sql = 'select * from student where s_name="小帅790";'
# stus = db.session.execute(sql)
# 使用filter
# stus = Student.query.filter(Student.s_name == '小帅790')
# 使用filter_by
stus = Student.query.filter_by(s_name='小帅399')
print(stus.first())
return render_template('studentlist.html', stus=stus)
# 更新方法
@stu.route('/updatestu/')
def update_stu():
# 第一种方式
# stu = Student.query.filter_by(s_id=5).first()
# stu.s_name = '李二狗'
# 第二种方法
Student.query.filter_by(s_id=5).update({'s_name': '王大锤'})
db.session.commit()
return redirect(url_for('stu.stu_all'))
# 删除方法
@stu.route('/deletestu/')
def delete_stu():
stu = Student.query.filter(Student.s_id == 5).first()
db.session.delete(stu)
db.session.commit()
return redirect(url_for('stu.stu_all'))