一个通用数据库连接插件
Project description
通用 ORM 数据库连接插件
- 参考 example_bot 来创建一个 聊天记录 插件吧~!
快速上手
新建 models.py
from tortoise import fields
from tortoise.models import Model
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")
from nonebot_plugin_tortoise_orm import add_model
# 插件存放结构
# src/plugins/__init__.py
# src/plugins/models.py
add_model("src.plugin.models")
# 如果以包进行安装,例如 nonebot_plugin_word_bank3
# add_model("nonebot_plugin_word_bank3.models")
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()
以上就是最简用法
配置
.env
设置
参考配置:
# db_url=postgres://postgres@localhost:5432/postgres
db_url=sqlite://db.sqlite3
db_url
使用 sqlite
直接使用相对路径来建立
db_url=sqlite://db.sqlite3
如果时指定路径,则应该是
db_url=sqlite:///data/db.sqlite
使用绝对路径 注意有三个 /
使用 PostgreSQL
db_url=postgres://postgres:pass@db.host:5432/somedb
- 说明:
postgres://
表示协议 postgres:pass@
表示登入账号和密码 如果没有密码则用postgres@
db.host:5432
表示数据库的地址 和 端口 如果是本机 则为localhost:5432
/somedb
表示数据库名
使用 MySQL/MariaDB
db_url=mysql://myuser:mypass@db.host:3306/somedb
跟上面的差不多
数据库类型
- postgres
- sqlite
- MySQL/MariaDB
其他待补充
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for nonebot_plugin_tortoise_orm-0.1.0b2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb6db76b568de52ed7e7b0851eca07dd356e443b5a171c625ea0d2135555da8e |
|
MD5 | fcb4f9b8f3bbdd2305f1ba18650a3c90 |
|
BLAKE2b-256 | 18dfd56d68dc51dcdfcd60eef613a771caa519fcd8654c7e747c236c55d156f6 |
Close
Hashes for nonebot_plugin_tortoise_orm-0.1.0b2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e804a40075481bfb56ad5d30c99799b148af3a5c3ee8f78680a8414d68bbbac |
|
MD5 | a5c97ba26a8ddb0e6b9d4e549864aa4f |
|
BLAKE2b-256 | ca35fe8d566280efbb5ce3b93c8554f5fe1181b31056e42a8c54fdef6bca2551 |