flask中配置mysql数据步骤
flask 应用配置mysql数据库:
- 需要下载mysql驱动引擎:
pip install pymysql
- 编写配置信息 config.py:
DEBUG = True DIALCT = 'mysql' DRIVER = 'pymysql' USERNAME = 'root' PASSWORD = 'root112233' HOST = '127.0.0.1' PORT = '3306' DBNAME = 'watchlist' SQLALCHEMY_DATABASE_URI = '{}+{}://{}:{}@{}:{}/{}?charset=utf8'.format(DIALCT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DBNAME) SQLALCHEMY_TRACK_MODIFICATIONS = True
- 在app.py中导入包,配置:
from flask import Flask from flask_sqlalchemy import SQLAlchemy import config app = Flask(__name__) app.config.from_object(config) db = SQLAlchemy(app) # 初始化扩展,传入程序实例 app
具体截图如下:
本文来自博客园,作者:热爱技术的小牛,转载请注明原文链接:https://www.cnblogs.com/my-blogs-for-everone/p/15617979.html