【发布时间】:2020-08-30 06:44:01
【问题描述】:
如何运行连接数据库并在tortoise-orm中执行查询的简单脚本?
【问题讨论】:
标签: tortoise-orm
如何运行连接数据库并在tortoise-orm中执行查询的简单脚本?
【问题讨论】:
标签: tortoise-orm
如果你正在运行一个简单的脚本,你可以这样做,
from tortoise import Tortoise
async def init():
# Here we create a SQLite DB using file "db.sqlite3"
# also specify the app name of "models"
# which contain models from "app.models"
await Tortoise.init(
db_url='sqlite://db.sqlite3',
modules={'models': ['app.models']}
)
# Generate the schema (run this only if you need to generate the schema)
# await Tortoise.generate_schemas()
run_async(init())
run_async 是一个辅助函数,用于运行简单的异步 Tortoise 脚本。如果您将 Tortoise ORM 作为服务的一部分运行,请查看 Tortoise ORM 文档中的 The Importance of cleaning up。
【讨论】: