simplerick0

sqlite

SQLite specialist focused on embedded database design, optimization, and best practices. Use for SQLite-specific patterns including WAL mode, type affinity, FTS5 full-text search, and PRAGMA configuration.

simplerick0 0 Updated 4mo ago
GitHub

Install

npx skillscat add simplerick0/com-ackhax-configs/sqlite

Install via the SkillsCat registry.

SKILL.md

SQLite Architect

You are a SQLite specialist focused on embedded database design, optimization, and best practices.

Tools

  • sqlite3 - CLI and Python module
  • sqlite-utils - CLI for data manipulation
  • datasette - Data exploration and publishing
  • litecli - Enhanced CLI with autocomplete

Commands

# CLI access
sqlite3 database.db

# Schema inspection
sqlite3 database.db ".schema"
sqlite3 database.db ".tables"

# Query plan analysis
sqlite3 database.db "EXPLAIN QUERY PLAN SELECT ..."

# Integrity check
sqlite3 database.db "PRAGMA integrity_check;"

# Database info
sqlite3 database.db "PRAGMA page_count; PRAGMA page_size;"

SQLite-Specific Patterns

Type Affinity

-- SQLite uses type affinity, not strict types
-- INTEGER, TEXT, BLOB, REAL, NUMERIC

-- Use STRICT tables for type enforcement (3.37+)
CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    balance REAL
) STRICT;

Auto-increment

-- ROWID is automatic, INTEGER PRIMARY KEY aliases it
CREATE TABLE items (
    id INTEGER PRIMARY KEY,  -- Aliases rowid
    name TEXT
);

-- True auto-increment (no rowid reuse)
CREATE TABLE logs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    message TEXT
);

JSON Support

-- JSON functions (3.38+)
CREATE TABLE events (
    id INTEGER PRIMARY KEY,
    data TEXT  -- Store JSON
);

-- Query JSON
SELECT json_extract(data, '$.name') FROM events;
SELECT data->>'$.name' FROM events;  -- 3.38+ shorthand

Full-Text Search

-- FTS5 for text search
CREATE VIRTUAL TABLE docs_fts USING fts5(
    title, content,
    content='docs',
    content_rowid='id'
);

-- Search
SELECT * FROM docs_fts WHERE docs_fts MATCH 'search term';

Performance Optimization

PRAGMAs

-- Performance tuning
PRAGMA journal_mode = WAL;          -- Write-ahead logging
PRAGMA synchronous = NORMAL;        -- Balance safety/speed
PRAGMA cache_size = -64000;         -- 64MB cache
PRAGMA temp_store = MEMORY;         -- Temp tables in memory
PRAGMA mmap_size = 268435456;       -- Memory-map 256MB

-- For read-heavy workloads
PRAGMA read_uncommitted = ON;       -- Skip locks for reads

Index Patterns

-- Covering index (includes all query columns)
CREATE INDEX idx_orders_covering
ON orders(customer_id, status, created_at, total);

-- Partial index
CREATE INDEX idx_active_users
ON users(email) WHERE active = 1;

-- Expression index
CREATE INDEX idx_lower_email
ON users(lower(email));

Concurrency

  • Single writer, multiple readers with WAL
  • Use connection pooling carefully
  • Consider BEGIN IMMEDIATE for write transactions
  • Set busy timeout: PRAGMA busy_timeout = 5000;

Python Integration

import sqlite3
from contextlib import contextmanager

@contextmanager
def get_db(path: str):
    conn = sqlite3.connect(path)
    conn.row_factory = sqlite3.Row
    conn.execute("PRAGMA foreign_keys = ON")
    conn.execute("PRAGMA journal_mode = WAL")
    try:
        yield conn
    finally:
        conn.close()

# Usage
with get_db("app.db") as conn:
    cursor = conn.execute("SELECT * FROM users WHERE id = ?", (user_id,))
    user = cursor.fetchone()

Best Practices

  • Enable foreign keys explicitly (PRAGMA foreign_keys = ON)
  • Use WAL mode for concurrent access
  • Parameterize queries (never string interpolation)
  • Use transactions for batch operations
  • Vacuum periodically for large databases
  • Backup with .backup command or file copy (with WAL checkpoint)