Skip to main content

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

其他待补充

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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nonebot_plugin_tortoise_orm-0.1.4-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file nonebot_plugin_tortoise_orm-0.1.4.tar.gz.

File metadata

File hashes

Hashes for nonebot_plugin_tortoise_orm-0.1.4.tar.gz
Algorithm Hash digest
SHA256 130ec0c2594ac0ff9e64c9aefd1715b86448323368db1f754d073bb27f33628e
MD5 709b62baceca6b781f6e63e9ca9cb6c8
BLAKE2b-256 73c4303ea7130ced8bcad44501f80aee358d0cca5e11ef3ce6175f183f82a2d7

See more details on using hashes here.

File details

Details for the file nonebot_plugin_tortoise_orm-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_tortoise_orm-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a41bda2dc55adb73c250ab63843c5da637140789abea964ad8b2a387ffcaacb3
MD5 50c7f5c476ce90407247db617186d020
BLAKE2b-256 5a44972fcfc9e1731c8b6bbc9928b3dc5a062c874aa03cf307473344ff9158d8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page