Python2的Flask使用数据库链接,弹出警告SQLALCHEMY_DATABASE_URI、SQLALCHEMY_TRACK_MODIFICATI,配置app.config还有警告

弹出以下警告

/home/python/.virtualenvs/Flask_py/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:819: UserWarning: SQLALCHEMY_DATABASE_URI not set. Defaulting to "sqlite:///:memory:".
  'SQLALCHEMY_DATABASE_URI not set. Defaulting to '
/home/python/.virtualenvs/Flask_py/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
/home/python/.virtualenvs/Flask_py/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:819: UserWarning: SQLALCHEMY_DATABASE_URI not set. Defaulting to "sqlite:///:memory:".
  'SQLALCHEMY_DATABASE_URI not set. Defaulting to '
/home/python/.virtualenvs/Flask_py/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
 * Debugger is active!
 * Debugger PIN: 401-040-862

1.检查是否有配置

# 指定使用的数据库的链接地址
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:[email protected]:3306/test8"
# 关闭追踪数据库的修改
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

2.如果有上面两句代码还是有警告,就检查是否代码顺序有问题,需要先配置app.config,再实例化对象

错误顺序

# 创建一个SQLAlchemy对象,需要放在config后面
db = SQLAlchemy(app)
# 指定使用的数据库的链接地址
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:[email protected]:3306/test8"
# 关闭追踪数据库的修改
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

正确顺序

# 指定使用的数据库的链接地址
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:[email protected]:3306/test8"
# 关闭追踪数据库的修改
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

# 创建一个SQLAlchemy对象,需要放在config后面
db = SQLAlchemy(app)

你可能感兴趣的:(Python,Flask)