---恢复内容开始---
首先要安装 flask-sqlalchemy
数据库连接设置
在flask-SQLAlchemy中,数据库使用URL指定,而且程序使用的数据库必须保存到flask配置对象的SQLALCHEMY_DATABASE_URL键中
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:mysql@127.0.0.1:3306/xinagmu?charset=utf8'
划横线哪里填库的名称,root后面跟数据库的密码
其他相关配置
相关的增删改查
from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config.from_pyfile('config.ini') # 建立数据库对象 db = SQLAlchemy(app) # 建立数据库类,用来映射数据库,将数据库的模型作为参数传入 class User(db.Model): # 声明表明 __tablename__ = 'user' # 建立字段数 Column字段名 固定写法 id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) password = db.Column(db.String(200)) # 数据库的入库操作 @app.route("/") def index(): # 增加,入库逻辑 # 声明一个对象 # user = User(name='ssss', password='ssss') # # 调用 添加方法 # db.session.add(user) return '这是首页' # 数据库的删除操作 @app.route('/del') def del_(): # 根据某个字段做删除,filter_by可以理解为where 条件限定 # 翻译为delete from user where id = 1 User.query.filter_by(id=1).delete() return '删除' # 数据库的修改操作 @app.route('/edit') def edit(): # 根据某个字段做修改操作 # 翻译为 update user set name = '张三' where id = 2 User.query.filter_by(id=4).update({'name': 'susu'}) return '修改' # 数据库的查询操作 @app.route('/select') def select(): # 简单的的全量查询 # 翻译为select *from user ulist = User.query.all() print(ulist) for i in ulist: print(i.name) # 只取一条 # 翻译为 select *from user limit 1 # ulist = User.query.first() # print(ulist) # 使用原生的sql语句 # 翻译为select * from user order by id desc limit 1,2 items = db.session.execute('select * from user order by id desc limit 1,2') # 将结果集强转为list items = list(items) print(items) # 使用原生sql语句进行修改操作 # db.session.execute("update user set password='1234' where id=5 ") return '修改'if __name__ == "__main__": app.run()
小案例:
from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config.from_pyfile('config.ini') app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:mysql@127.0.0.1:3306/xinagmu?charset=utf8' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True db = SQLAlchemy(app) class Movies(db.Model): id = db.Column(db.Integer, primary_key=True) photo = db.Column(db.String(200)) title = db.Column(db.String(100)) score = db.Column(db.String(100)) evalueate = db.Column(db.String(100)) @app.route('/') def index(): items = db.session.execute('select *from movies') items = list(items) print(items) return render_template('movies.html', items=items) if __name__ == "__main__": app.run()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>movies</title> </head> <body> <table border="1"> <tr> <td>id</td> <td>photo</td> <td>title</td> <td>score</td> <td>evalueate</td> </tr> {% for i in items %} <tr> <td>{{ i.id }}</td> <td><img src="{{ i.photo }}" /></td> <td>{{ i.title }}</td> <td>{{ i.score }}</td> <td>{{ i.evalueate }}</td> </tr> {% endfor %} </table> </body> </html>
效果:
---恢复内容结束---