赞
踩
pip install flask-sqlalchemy
pip install flask-mysqldb
class Config(object):
"""配置参数"""
# sqlalchemy的配置参数
SQLALCHEMY_DATABASE_URI = "mysql://root:root@127.0.0.1:3306/flask_demo"
# 设置sqlalchemy自动更跟踪数据库
SQLALCHEMY_TRACK_MODIFICATIONS = True
# 设置每次请求结束后会自动提交数据库中的改动
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
# 查询时会显示原始SQL语句
SQLALCHEMY_ECHO = True
app.config.from_object(Config)
db = SQLAlchemy(app)
注意:需要在命令行操作
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) class Config(object): """配置参数""" # sqlalchemy的配置参数 SQLALCHEMY_DATABASE_URI = "mysql://root:root@127.0.0.1:3306/flask_demo" # 设置sqlalchemy自动更跟踪数据库 SQLALCHEMY_TRACK_MODIFICATIONS = True # 设置每次请求结束后会自动提交数据库中的改动 SQLALCHEMY_COMMIT_ON_TEARDOWN = True # 查询时会显示原始SQL语句 SQLALCHEMY_ECHO = True app.config.from_object(Config) db = SQLAlchemy(app) # 定义数据库的模型 class Author(db.Model): """作者""" __tablename__ = "author" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32), unique=True) books = db.relationship("Book", backref="author") class Book(db.Model): """书籍""" __tablename__ = "book" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) author_id = db.Column(db.Integer, db.ForeignKey("author.id")) if __name__ == '__main__': db.drop_all() #第一次执行的时候操作 db.create_all()#创建表 # app.run(debug=True)
def add_one(): """添加单个数据""" user1 = Author(name="fly1") db.session.add(user1) db.session.commit() bk_user1 = Book(name='fly自传', author_id=user1.id) db.session.add(bk_user1) db.session.commit() def add_many(): """添加多个数据""" au_xi = Author(name='我吃西红柿') au_qian = Author(name='萧潜') au_san = Author(name='唐家三少') db.session.add_all([au_xi, au_qian, au_san]) db.session.commit() bk_xi = Book(name='吞噬星空', author_id=au_xi.id) bk_xi2 = Book(name='寸芒', author_id=au_qian.id) bk_qian = Book(name='飘渺之旅', author_id=au_qian.id) bk_san = Book(name='冰火魔厨', author_id=au_san.id) db.session.add_all([bk_xi, bk_xi2, bk_qian, bk_san]) db.session.commit()
def delete():
"""删除数据"""
author = Author.query.filter_by(name="唐家三少").first()
db.session.delete(author)
db.session.commit()
def update():
"""更新数据"""
book = Book.query.filter_by(name="吞噬星空").first()
book.name = '吞噬星空2'
db.session.commit()
Author.query.filter_by(name='zhang').update({'name': 'li'})
pass
def query():
"""查询数据"""
# r1 = Book.query.all() #查询所有
# r2 = Book.query.filter_by(name="吞噬星空").all() #根据条件查询
r3 = Book.query.filter_by(name="吞噬星空").first() # 根据条件查询 第一个
# r4 = Book.query.first() #查询第一个
# r5 = Book.query.filter(Author.name.endswith('g')).all() #模糊查询
print(r3)
print(type(r3))
在开发过程中,需要修改数据库模型,这样就可以使用Flask-Migrate来进行数据的迁移。
pip install flask-migrate
pip install flask_script
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate,MigrateCommand from flask_script import Shell,Manager app = Flask(__name__) manager = Manager(app) class Config(object): """配置参数""" # sqlalchemy的配置参数 SQLALCHEMY_DATABASE_URI = "mysql://root:root@127.0.0.1:3306/flask_demo" # 设置sqlalchemy自动更跟踪数据库 SQLALCHEMY_TRACK_MODIFICATIONS = True # 设置每次请求结束后会自动提交数据库中的改动 SQLALCHEMY_COMMIT_ON_TEARDOWN = True # 查询时会显示原始SQL语句 SQLALCHEMY_ECHO = True app.config.from_object(Config) db = SQLAlchemy(app) #第一个参数是Flask的实例,第二个参数是Sqlalchemy数据库实例 migrate = Migrate(app,db) #manager是Flask-Script的实例,这条语句在flask-Script中添加一个db命令 manager.add_command('db',MigrateCommand) # 定义数据库的模型 class Author(db.Model): """作者""" __tablename__ = "author" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(32), unique=True) books = db.relationship("Book", backref="author") class Book(db.Model): """书籍""" __tablename__ = "book" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) author_id = db.Column(db.Integer, db.ForeignKey("author.id")) if __name__ == '__main__': manager.run()
python app.py db init
python app.py db migrate -m ‘initial migration’
python app.py db upgrade
python database.py db downgrade 版本号
结束!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。