flask 配置数据库

from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_migrate import MigrateCommand

#创建应用实例
app = Flask(__name__)

manage = Manager(app)

#配置数据库(mysql)
app.config['SQLALCHEMY_DATABASE_URI']="mysql+pymysql://root:123456@localhost:3306/flask_1805"

# # 配置数据连接地址
# base_dir = os.path.dirname(__file__)
# database_uri = 'sqlite:///' + os.path.join(base_dir, 'data.sqlite')

#禁止数据的修改追踪(需要消耗资源)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False


#创建数据库操作对象
db = SQLAlchemy(app)

#创建数据库迁移对象
migrate = Migrate(app=app,db=db)

#将迁移命令添加到终端
manage.add_command('db',MigrateCommand)

#创建Model类
class School(db.Model):
    #给表重命名
    _tablename_ = 'school'

    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(20), unique=True)
if __name__ == '__main__':
    manage.run()

你可能感兴趣的:(flask 配置数据库)