在应用程序上下文之外创建数据库

编程入门 行业动态 更新时间:2024-10-28 20:24:24
本文介绍了在应用程序上下文之外创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个像这样的应用程序工厂

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

更多推荐

在应用程序上下文之外创建数据库

本文发布于:2023-10-30 03:12:28,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   应用程序   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!