本文介绍了在应用程序上下文之外创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的应用程序工厂 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()这是从文档这里和这里。 / p>
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 appthen, 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.
更多推荐
在应用程序上下文之外创建数据库
发布评论