【星海出品】SQLite的进阶操作

因为去年到今年初和一些DBA做了一些大型项目,深刻的了解了数据库操作的运维与可靠性操作是非常消耗精力和时间的。所以这篇文章用来记录一些数据库的操作,方便后续直接使用。

因为今年一直在做Hermes和OpenClaw以及一些自己的开源项目,所以用到SQLite 比较多,由于深刻的认识到传统的大型多并发数据库要做好是需要非常大的精力去操作的,所以后续一些个人文章非大型场景且专题数据库的操作,都会用SQLite来进行。

重新建表,保留表中原来数据,主要为重新增加主键问题

bash 复制代码
-- 直接在文件层面复制 english_learning.db

-- 建新表(带 domain,主键含领域)
CREATE TABLE MemoryNodes_new (
    user_id INTEGER NOT NULL,
    word_id INTEGER NOT NULL,
    domain TEXT NOT NULL DEFAULT 'web_programming',
    review_count INTEGER DEFAULT 1,
    ease_factor REAL DEFAULT 2.5,
    interval_days INTEGER DEFAULT 1,
    memory_strength REAL DEFAULT 0.5,
    forgetting_curve REAL DEFAULT 1.0,
    next_review_at DATETIME,
    memory_status TEXT DEFAULT 'learning',
    difficulty_score REAL DEFAULT 0.5,
    priority_score REAL DEFAULT 0.5,
    is_active INTEGER DEFAULT 1,
    word_root_score REAL DEFAULT 0.5,
    word_affix_score REAL DEFAULT 0.5,
    etymology_score REAL DEFAULT 0.5,
    spelling_score REAL DEFAULT 0.5,
    pos_score REAL DEFAULT 0.5,
    context_score REAL DEFAULT 0.5,
    frequency_score REAL DEFAULT 0.5,
    consecutive_correct INTEGER DEFAULT 0,
    consecutive_wrong INTEGER DEFAULT 0,
    static_priority_score REAL DEFAULT 0.0,
    last_reviewed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (user_id, domain, word_id),
    FOREIGN KEY (user_id) REFERENCES Users(user_id) ON DELETE CASCADE,
    FOREIGN KEY (word_id) REFERENCES Words(word_id) ON DELETE CASCADE
);

-- 迁移旧数据(存量行归到默认领域 web_programming)
INSERT INTO MemoryNodes_new
    (user_id, word_id, domain, review_count, ease_factor, interval_days,
     memory_strength, forgetting_curve, next_review_at, memory_status,
     difficulty_score, priority_score, is_active, word_root_score, word_affix_score,
     etymology_score, spelling_score, pos_score, context_score, frequency_score,
     consecutive_correct, consecutive_wrong, static_priority_score,
     last_reviewed_at, created_at, updated_at)
SELECT
    user_id, word_id, 'web_programming', review_count, ease_factor, interval_days,
    memory_strength, forgetting_curve, next_review_at, memory_status,
    difficulty_score, priority_score, is_active, word_root_score, word_affix_score,
    etymology_score, spelling_score, pos_score, context_score, frequency_score,
    consecutive_correct, consecutive_wrong, static_priority_score,
    last_reviewed_at, created_at, updated_at
FROM MemoryNodes;

-- 换表
DROP TABLE MemoryNodes;
ALTER TABLE MemoryNodes_new RENAME TO MemoryNodes;

-- 重建索引(原 idx_memory_user 已随旧表删除)
CREATE INDEX IF NOT EXISTS idx_memory_user ON MemoryNodes(user_id);
CREATE INDEX IF NOT EXISTS idx_memory_user_domain ON MemoryNodes(user_id, domain);
相关推荐
小此方3 小时前
Re:Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
xszivvvv3 小时前
Mariadb数据库frm文件异常,如何恢复数据
数据库·mariadb
smile_life_4 小时前
MySQL 设置 防止账户被暴力破解
数据库·mysql
ShineWinsu4 小时前
对于Qt:QRadioButton常见属性以及QButtonGroup的解析
数据库·c++·qt·microsoft·gui·qradiobutton·qbuttongroup
测试盐5 小时前
oracle数据库卡顿排查
数据库·oracle
滕州市燕猫虎计算机科技工作室个体工商户5 小时前
MySQL、MVCC、多版本并发控制
数据库·mysql·mvcc·数据库事务
明航咨询-陈老师5 小时前
ITSS运行维护服务能力成熟度认证技术纵深:标准模型、等级路径、2026审核趋势与CS/CMMI对比
服务器·数据库·cmmi
缓慢更新6 小时前
AI 辅助生成单测:从接口定义到用例的流水线
java·服务器·前端·网络·数据库·人工智能
AI小百科6 小时前
向量数据库:AI 的「记忆仓库」是怎么工作的
数据库·人工智能·oracle
煎饼皮皮侠6 小时前
【设计】设计一个Web版数据库管理平台(之七):多数据库类型易扩展
数据库