flask中配置mysql数据步骤

flask 应用配置mysql数据库:

  1. 需要下载mysql驱动引擎: 
    pip install pymysql
    

     

  2. 编写配置信息 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
    

      

  3. 在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
    

      

具体截图如下:

 

 

 

 

 

posted @ 2021-11-29 09:34  热爱技术的小牛  阅读(518)  评论(0编辑  收藏  举报
热爱技术的小牛