一个通用数据库连接插件
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.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d669ecd1ea6e78526b9408400c356e228db99d57193d63858d851cc2bbe5fad |
|
MD5 | bc3a05c7f0a696fe341e031795b6daed |
|
BLAKE2b-256 | 7b3f544813906f77c71ba8ec7b39ba7ca500ff9e634b45d349f105aac9a2b05e |
Close
Hashes for nonebot_plugin_tortoise_orm-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86ab0849c2f01dfdfbdca6442b9f30dc48471ebd29777131fb4c1fff2756afae |
|
MD5 | e805c49b61d88d8e9a2ed689c9385dfb |
|
BLAKE2b-256 | 436fe9a24772702c92d8123a9b0c7bf2013fd09825e91f9a1b568386fd242c99 |