def get_db(self,dbfile):
if hasattr(g, 'sqlite_db'): self.close_db(g.sqlite_db)
try:
g.sqlite_db = self.connect_db('{}/{}'.format(app.root_path, dbfile))
except sqlite3.OperationalError as e:
raise e
return g.sqlite_db
嗨,这段代码位于DB类内部,我得到的错误是
RuntimeError:在应用程序上下文之外工作
该行发生错误
g.sqlite_db = self.connect_db('{}/{}'.format(app.root_path, dbfile))
我认为问题在于g,它像这样导入 from flask import g
如何解决此错误?谢谢。
从Flask源代码中flask/globals.py
:
_app_ctx_err_msg = '''\
Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in a way. To solve
this set up an application context with app.app_context(). See the
documentation for more information.\
'''
通过该文档,您可以看到您需要flask.current_app
指向您的应用程序,而当前并不需要。
您可能在Flask初始化之前调用了DB函数。我的猜测是您的app
对象尚未使用Flask
构造函数创建。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句