You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQLite中statistic表guild_id与category组合唯一行插入需求

解决方法
  • 修正SQL字段名错误
    你的表字段是guild_id,但代码里误写为statistic_channel_id,这会导致字段不匹配,必须替换为正确的字段名。

  • 添加联合唯一约束
    要实现guild_id与category组合唯一的要求,需要给这两个字段创建联合唯一约束,否则INSERT OR IGNORE无法识别重复项。如果表还没设置该约束,先执行以下SQL:

-- 创建联合唯一索引
CREATE UNIQUE INDEX idx_guild_category ON statistic(guild_id, category);

或者在创建表时直接声明约束:

CREATE TABLE statistic (
    guild_id INT,
    category TEXT,
    number INT,
    UNIQUE(guild_id, category)
);
  • 修正后的函数代码
def save_statistic(self, category: str, guild_id: int):
    self.c.execute('''INSERT OR IGNORE INTO statistic 
                   (guild_id, category, number) 
                   VALUES (?, ?, 0)
                   ''', (guild_id, category))

内容的提问来源于stack exchange,提问作者sndmndss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:44:51