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

其他待补充

Metadata

Release files for nonebot-plugin-tortoise-orm 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nonebot-plugin-tortoise-orm 0.1.4
File Size Uploaded
nonebot_plugin_tortoise_orm-0.1.4.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nonebot-plugin-tortoise-orm 0.1.4
File Interpreter ABI Platform
nonebot_plugin_tortoise_orm-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / nonebot_plugin_tortoise_orm-0.1.4.tar.gz

Download URL nonebot_plugin_tortoise_orm-0.1.4.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
130ec0c2594ac0ff9e64c9aefd1715b86448323368db1f754d073bb27f33628e
BLAKE2b-256 checksum
How to use checksums
73c4303ea7130ced8bcad44501f80aee358d0cca5e11ef3ce6175f183f82a2d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / nonebot_plugin_tortoise_orm-0.1.4-py3-none-any.whl

Download URL nonebot_plugin_tortoise_orm-0.1.4-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a41bda2dc55adb73c250ab63843c5da637140789abea964ad8b2a387ffcaacb3
BLAKE2b-256 checksum
How to use checksums
5a44972fcfc9e1731c8b6bbc9928b3dc5a062c874aa03cf307473344ff9158d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page