Skip to main content

一个通用数据库连接插件

Project description

NoneBotPluginLogo

nonebot-plugin-tortoise-orm

✨ 通用 ORM 数据库连接插件 ✨

license pypi python

  • 参考 example_bot 来创建一个 聊天记录 插件吧~!

快速上手

新建 models.py

from tortoise import fields
from tortoise.models import Model

# 导入插件方法
from nonebot_plugin_tortoise_orm import add_model


add_model("src.plugins.models")
# 如果以包/插件的方式,例如 nonebot_plugin_word_bank3
# add_model("nonebot_plugin_word_bank3.models")
# 或
# add_model("__name__")
class TestTable(Model):
    message_id = fields.BigIntField(pk=True)
    text = fields.TextField()


    class Meta:
        table = "test_table"
        table_description = "测试标题" # 可选

__init__.py 中加入引用

from nonebot import require

require("nonebot_plugin_tortoise_orm")

# 插件存放结构
# src/plugins/__init__.py
# src/plugins/models.py

from .models import TestTable

直接使用

参考 tortoise models

# 创建
await TestTable.create(message_id=114514)
await TestTable.update_or_create(message_id=114514)
await TestTable.get_or_create(message_id=114514)

# 获取
await TestTable.get(message_id=114514)
await TestTable.get_or_none(message_id=114514)

# 更改
if record := await TestTable.get_or_none(message_id=114514):
    record.text = "1919810"
    await record.save()

# 删除
if record := await TestTable.get_or_none(message_id=114514):
    await record.delete()
    await record.save()

以上就是最简用法

多个数据库

在某些特殊情况下,可以不使用默认数据库,add_model 的参数即可实现

  • db_name 自行确认
  • db_url 地址同下数据库URL
add_model(
    __name__,
    db_name="chatrecorder",
    db_url="sqlite://data/chatrecorder.db",
)

.env 设置

参考配置:

tortoise_orm_db_url

# tortoise_orm_db_url=postgres://postgres@localhost:5432/postgres
tortoise_orm_db_url=sqlite://db.sqlite3

tortoise_orm_db_url

使用 sqlite

直接使用相对路径来建立

tortoise_orm_db_url=sqlite://db.sqlite3

如果时指定路径,则应该是

tortoise_orm_db_url=sqlite:///data/db.sqlite

使用绝对路径 注意有三个 /

使用 PostgreSQL

tortoise_orm_db_url=postgres://postgres:pass@db.host:5432/somedb
  • 说明: postgres:// 表示协议
  • postgres:pass@ 表示登入账号和密码 如果没有密码则用 postgres@
  • db.host:5432 表示数据库的地址 和 端口 如果是本机 则为 localhost:5432
  • /somedb 表示数据库名

使用 MySQL/MariaDB

tortoise_orm_db_url=mysql://myuser:mypass@db.host:3306/somedb

跟上面的差不多

数据库类型

  • postgres
  • sqlite
  • MySQL/MariaDB

其他待补充

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nonebot_plugin_tortoise_orm-0.1.4.tar.gz (4.6 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page