【问题标题】:How to run simple scripts using tortoise-orm?如何使用 tortoise-orm 运行简单的脚本?
【发布时间】:2020-08-30 06:44:01
【问题描述】:

如何运行连接数据库并在tortoise-orm中执行查询的简单脚本?

【问题讨论】:

    标签: tortoise-orm


    【解决方案1】:

    如果你正在运行一个简单的脚本,你可以这样做,

    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

    【讨论】:

      猜你喜欢
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 2013-01-15
      • 2016-11-08
      • 2021-04-19
      • 2021-04-26
      • 1970-01-01
      相关资源
      最近更新 更多