flask中数据库初始化
1 from flask import Flask 2 # 一,导入sqlalchemy 3 from flask_sqlalchemy import SQLAlchemy 4 # 二,实例化flask 5 app = Flask(__name__) 6 7 # 三,定义一个配置类,里面类变量SQLALCHEMY_DATABASE_URI配置好 8 class DevelopConfig(): 9 10 DEBUG = True 11 12 DATABASE = { 13 "ENGINE": "mysql", 14 "DRIVER": "pymysql", 15 "USER": "root", 16 "PASSWORD": "123456", 17 "HOST": "127.0.0.1", 18 "PORT": "3306", 19 "NAME": "shop" 20 } 21 22 SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}".format("mysql", "pymysql", "root", "123456", "127.0.0.1", "3306","shop") 23 24 # 四,将配置类导入到app.config中 25 app.config.from_object(obj=DevelopConfig) 26 # 五,实例化SQLAlchemy 27 db=SQLAlchemy(app)
这样,数据库初始化完成,为了方便,这写到了一个文件中,实际生产中,会单独配置一个config文件,把开发环境和生产环境分别写进去
全世界的程序员们联合起来吧!