在应用程序上下文之外创建数据库 [英] creating a database outside the application context
本文介绍了在应用程序上下文之外创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
db = SQLAlchemy()
def create_app环境):
app = Flask(__ name__)
app.config.from_object(config [environment])
db.init_app(app)
#... etc
$ b返回应用程序
然后,我有一个脚本,的应用程序的上下文。这个脚本是一个每x小时运行一次的cron。我想更新sqlite数据库,以便应用程序正在使用。这是可能的吗?
解决方案
Flask-SQLAlchemy只需要一个应用上下文来操作。
app = create_app(env)
ctx = app.app_context()
ctx.push()
#你的代码在这里
ctx.pop()
I have an app factory like so
db = SQLAlchemy()
def create_app(environment):
app = Flask(__name__)
app.config.from_object(config[environment])
db.init_app(app)
# ... etc
return app
then, I have a script which fetches CSVs outside of the context of the application. This script is a cron which is run every x hours
I want to update the sqlite database somehow that the application is using. Is this possible?
解决方案
Flask-SQLAlchemy only needs an app context to operate. You can create an app context manually.
app = create_app(env)
ctx = app.app_context()
ctx.push()
# your code here
ctx.pop()
This is from the docs here and here.
这篇关于在应用程序上下文之外创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文