nonebot-plugin-tortoise-orm
nonebot-plugin-tortoise-orm copied to clipboard
一个通用数据库连接插件
nonebot-plugin-tortoise-orm
✨ 通用 ORM 数据库连接插件 ✨
- 参考 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
直接使用
# 创建
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=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:[email protected]:5432/somedb
- 说明:
postgres://表示协议 postgres:pass@表示登入账号和密码 如果没有密码则用postgres@db.host:5432表示数据库的地址 和 端口 如果是本机 则为localhost:5432/somedb表示数据库名
使用 MySQL/MariaDB
tortoise_orm_db_url=mysql://myuser:[email protected]:3306/somedb
跟上面的差不多
数据库类型
- [x] postgres
- [x] sqlite
- [x] MySQL/MariaDB
其他待补充
