🦀 ClawHub
SQLite
by @ivangdavila
Use SQLite correctly with proper concurrency, pragmas, and type handling.
TERMINAL
clawhub install sqlite📖 About This Skill
name: SQLite description: Use SQLite correctly with proper concurrency, pragmas, and type handling. metadata: {"clawdbot":{"emoji":"🪶","requires":{"bins":["sqlite3"]},"os":["linux","darwin","win32"]}}
Concurrency (Biggest Gotcha)
PRAGMA journal_mode=WAL—allows reads during writes, huge improvementPRAGMA busy_timeout=5000—waits 5s before SQLITE_BUSY instead of failing immediately-wal and -shm files—don't forget to copy them with main databaseBEGIN IMMEDIATE to grab write lock early—prevents deadlocks in read-then-write patternsForeign Keys (Off by Default!)
PRAGMA foreign_keys=ON required per connection—not persisted in databasePRAGMA foreign_keys returns 0 or 1Type System
STRICT tables enforce types—but only SQLite 3.37+ (2021)Schema Changes
ALTER TABLE very limited—can add column, rename table/column; that's mostly itALTER TABLE ADD COLUMN can't have PRIMARY KEY, UNIQUE, or NOT NULL without defaultPerformance Pragmas
PRAGMA optimize before closing long-running connections—updates query planner statsPRAGMA cache_size=-64000 for 64MB cache—negative = KB; default very smallPRAGMA synchronous=NORMAL with WAL—good balance of safety and speedPRAGMA temp_store=MEMORY for temp tables in RAM—faster sorts and temp resultsVacuum & Maintenance
VACUUM rewrites entire database, reclaims spaceVACUUM needs 2x disk space temporarily—ensure enough roomPRAGMA auto_vacuum=INCREMENTAL with PRAGMA incremental_vacuum—partial reclaim without full rewriteBackup Safety
.backup command in sqlite3—or sqlite3_backup_* API-wal and -shm must be copied atomically with main fileVACUUM INTO 'backup.db' creates standalone copy (3.27+)Indexing
CREATE INDEX ... WHERE conditionCREATE INDEX ON t(lower(name))EXPLAIN QUERY PLAN shows index usage—simpler than PostgreSQL EXPLAINTransactions
BEGIN; INSERT...; INSERT...; COMMIT—10-100x fasterBEGIN EXCLUSIVE for exclusive lock—blocks all other connectionsSAVEPOINT name / RELEASE name / ROLLBACK TO nameCommon Mistakes
VACUUM can change ROWIDs; use explicit INTEGER PRIMARY KEY':memory:'—each connection gets different database; use file::memory:?cache=shared for shared