SQLite DB Exploration
用途
sqlite-explorer 用于在本地对 SQLite 数据库进行探索和查询。它适合需要查看数据库结构、列出表/列/索引,以及运行自定义 SQL 的场景。
使用约定
- 默认以只读方式探索(SELECT、schema 查询)。
- 仅在明确要求或确属必要时才运行写入查询(INSERT/UPDATE/DELETE/DDL)。
- 在编写复杂查询前,始终先列出表并检查 schema。
- 从用户提供的值构建 SQL 时,优先使用参数化查询(如适用)。
实现方式
该技能通过 Bun 脚本实现:
${SKILL_DIR}/scripts/sqlite-explorer.ts
该脚本必须使用 Bun 执行,接受命令行参数以指定数据库路径、查询和模式(只读或写入)。
用法
列出表: bun ${SKILL_DIR}/scripts/sqlite-explorer.ts ./path/to/db.sqlite --tables
显示某个表的 schema: bun ${SKILL_DIR}/scripts/sqlite-explorer.ts ./path/to/db.sqlite --schema users
运行任意 SQL 查询: bun sqlite-explorer.ts ./path/to/db.sqlite --query "SELECT * FROM users LIMIT 10"
运行写入查询(仅在需要时): bun ${SKILL_DIR}/scripts/sqlite-explorer.ts ./path/to/db.sqlite --query "UPDATE users SET active=1 WHERE id=42"
只读模式(推荐用于探索): bun ${SKILL_DIR}/scripts/sqlite-explorer.ts ./path/to/db.sqlite --readonly --query "SELECT COUNT(*) FROM users"
输出预期
- 表:每行一个表名
- Schema:列 + 主键 + 外键 + 索引
- 查询结果:以紧凑表格打印;对于非 SELECT 语句还会打印受影响的行数
有用的内置内省 SQL
表:SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY 1;
列:PRAGMA table_info('table_name');
外键:PRAGMA foreign_key_list('table_name');
索引:PRAGMA index_list('table_name'); + PRAGMA index_info('index_name');
行数:SELECT COUNT(*) AS count FROM table_name;