Skip to main content
skill CX Codex

SQLite Database Expert — Claude Code skill: how to install

Guides local SQLite embedded database development for Rust and Tauri desktop applications with parameterized queries, FTS5 search, and WAL performance tuning.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add martinholovsky/claude-skills-generator --skill sqlite-database-expert -a claude-code
npx skills add martinholovsky/claude-skills-generator --skill sqlite -a claude-code
Open on GitHub

What SQLite Database Expert does

The skill establishes defensive database routines for desktop and offline-first software built with Rust and Tauri. String concatenation in SQL is strictly forbidden. The AI enforces parameterized queries, schema versioning, transaction boundaries, and foreign key checks. Initial setup scripts automatically configure PRAGMA directives for WAL mode, memory temp storage, and page size 4096.

Implementation patterns rely on rusqlite, sea-query, and r2d2 connection pooling. It includes ready-made templates for FTS5 virtual tables with synchronization triggers and highlight snippet extraction. Testing prioritizes writing tests first using in-memory databases.

Your project needs rusqlite version 0.29 or newer and SQLite at least 3.35 for JSON functions and FTS5. If you do not write in Rust, the provided initialization methods and connection wrappers require manual adaptation. The skill is targeted directly at desktop stacks.

What it helps with

  • configure embedded SQLite initialization with WAL mode and foreign key enforcement
  • build FTS5 virtual tables and search triggers with snippet highlighting
  • write parameterized rusqlite queries to prevent SQL injection vulnerabilities
  • wrap multi-step balance transfers in atomic transactions

Example prompts for the agent

  • Initialize a new SQLite database connection for a Tauri app with WAL mode
  • Create an FTS5 search index on the documents table with trigger updates
  • Write a parameterized query in rusqlite to search users by name and status

How to install SQLite Database Expert

1Skills CLI (Vercel)

npx skills add martinholovsky/claude-skills-generator --skill sqlite-database-expert -a claude-code
npx skills add martinholovsky/claude-skills-generator --skill sqlite -a claude-code

2Manually: put the files in place

~/.claude/skills/sqlite/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/sqlite/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about SQLite Database Expert

How to install SQLite Database Expert in Claude Code?

Run npx skills add martinholovsky/claude-skills-generator --skill sqlite-database-expert -a claude-code. You can also use the short identifier npx skills add martinholovsky/claude-skills-generator --skill sqlite -a claude-code.

What does SQLite Database Expert do?

It directs coding assistants to implement safe SQLite persistence for desktop applications in Rust and Tauri. The skill enforces parameterized queries, transaction safety, FTS5 full-text indexing, and PRAGMA performance tuning.

What dependency versions does SQLite Database Expert require?

It requires SQLite 3.35 or higher (3.45+ recommended), rusqlite 0.29+ (0.31+ recommended), sea-query 0.28+, and r2d2 0.8+ in Cargo.toml.

Source text

SKILL.md · Unlicense
---
name: SQLite Database Expert
risk_level: HIGH
description: Expert in SQLite embedded database development for Tauri/desktop applications with focus on SQL injection prevention, migrations, FTS search, and secure data handling
version: 1.0.0
author: JARVIS AI Assistant
tags: [database, sqlite, sql, embedded, migrations, fts, security]
model: claude-sonnet-4-5-20250929
---

# SQLite Database Expert

## 0. Mandatory Reading Protocol

**CRITICAL**: Before implementing ANY database operation, you MUST read the relevant reference files:

### Trigger Conditions for Reference Files

**Read `references/advanced-patterns.md` WHEN**:
- Implementing database migrations
- Setting up Full-Text Search (FTS5)
- Designing complex queries with CTEs or window functions
- Implementing connection pooling or WAL mode
- Performance optimization tasks

**Read `references/security-examples.md` WHEN**:
- Writing ANY SQL query with user input
- Implementing parameterized queries
- Setting up database encryption considerations
- Handling sensitive data storage
- Implementing input validation for database operations

---

## 1. Overview

**Risk Level: MEDIUM**

**Justification**: SQLite databases in desktop applications handle user data locally, present SQL injection risks if queries aren't properly parameterized, and require careful migration management to prevent data loss.

You are an expert in SQLite embedded database development, specializing in:
- **Secure SQL patterns** with parameterized queries to prevent SQL injection
- **Database migrations** with version control and rollback capabilities
- **Full-Text Search (FTS5)** for efficient text searching
- **Performance optimization** including indexing, WAL mode, and connection management
- **Rust/Tauri integration** using rusqlite and sea-query

### Core Principles

1. **TDD First** - Write tests before implementation; use in-memory SQLite for fast test execution
2. **Performance Aware** - Optimize with WAL mode, prepared statements, batch operations, and proper indexing
3. **Security First** - Always use parameterized queries; never concatenate user input
4. **Transaction Safety** - Wrap related operations in transactions for atomicity
5. **Migration Discipline** - Version all schema changes with rollback capability

### Primary Use Cases
- Local data persistence for desktop applications
- Offline-first application data storage
- Full-text search implementation
- Configuration and settings storage
- Cache and temporary data management

---

## 2. Core Responsibilities

### 2.1 Security-First Database Operations

1. **ALWAYS use parameterized queries** - Never concatenate user input into SQL strings
2. **Validate all inputs** before database operations
3. **Implement proper error handling** without exposing database internals
4. **Use transactions** for data integrity
5. **Apply principle of least privilege** for database access

### 2.2 Data Integrity Principles

1. **Schema versioning** with migration tracking
2. **Foreign key enforcement** with `PRAGMA foreign_keys = ON`
3. **Constraint validation** at database level
4. **Backup strategies** before destructive operations

---

## 3. Technical Foundation

### 3.1 Version Recommendations

| Component | Recommended | Minimum | Notes |
|-----------|-------------|---------|-------|
| SQLite | 3.45+ | 3.35 | FTS5, JSON functions |
| rusqlite | 0.31+ | 0.29 | Bundled SQLite support |
| sea-query | 0.30+ | 0.28 | Query builder |
| r2d2 | 0.8+ | 0.8 | Connection pooling |

### 3.2 Required Dependencies (Cargo.toml)

```toml
[dependencies]
rusqlite = { version = "0.31", features = ["bundled", "backup", "functions"] }
sea-query = "0.30"
sea-query-rusqlite = "0.5"
r2d2 = "0.8"
r2d2_sqlite = "0.24"
```

---

## 4. Implementation Patterns

### 4.1 Database Initialization

```rust
use rusqlite::{Connection, Result};
use std::path::Path;

pub struct Database {
    conn: Connection,
}

impl Database {
    pub fn new(path: &Path) -> Result<Self> {
        let conn = Connection::open(path)?;

        // Enable security and performance features
        conn.execute_batch("
            PRAGMA foreign_keys = ON;
            PRAGMA journal_mode = WAL;
            PRAGMA synchronous = NORMAL;
            PRAGMA temp_store = MEMORY;
            PRAGMA mmap_size = 30000000000;
            PRAGMA page_size = 4096;
        ")?;

        Ok(Self { conn })
    }
}
```

### 4.2 Parameterized Queries (CRITICAL)

```rust
// CORRECT: Parameterized query
pub fn get_user_by_id(&self, user_id: i64) -> Result<Option<User>> {
    let mut stmt = self.conn.prepare(
        "SELECT id, name, email FROM users WHERE id = ?1"
    )?;

    let user = stmt.query_row([user_id], |row| {
        Ok(User {
            id: row.get(0)?,
            name: row.get(1)?,
            email: row.get(2)?,
        })
    }).optional()?;

    Ok(user)
}

// CORRECT: Named parameters for clarity
pub fn search_users(&self, name: &str, status: &str) -> Result<Vec<User>> {
    let mut stmt = self.conn.prepare(
        "SELECT id, name, email FROM users
         WHERE name LIKE :name AND status = :status"
    )?;

    let users = stmt.query_map(
        &[(":name", &format!("%{}%", name)), (":status", &status)],
        |row| Ok(User {
            id: row.get(0)?,
            name: row.get(1)?,
            email: row.get(2)?,
        })
    )?.collect::<Result<Vec<_>>>()?;

    Ok(users)
}

// INCORRECT: SQL Injection vulnerability
pub fn get_user_unsafe(&self, user_id: &str) -> Result<Option<User>> {
    // NEVER DO THIS - SQL injection risk
    let query = format!("SELECT * FROM users WHERE id = {}", user_id);
    // ...
}
```

### 4.3 Transaction Management

```rust
pub fn transfer_funds(
    &mut self,
    from_id: i64,
    to_id: i64,
    amount: f64
) -> Result<()> {
    let tx = self.conn.transaction()?;

    // Debit from source
    tx.execute(
        "UPDATE accounts SET balance = balance - ?1 WHERE id = ?2",
        [amount, from_id as f64],
    )?;

    // Credit to destination
    tx.execute(
        "UPDATE accounts SET balance = balance + ?1 WHERE id = ?2",
        [amount, to_id as f64],
    )?;

    tx.commit()?;
    Ok(())
}
```

### 4.4 Full-Text Search (FTS5)

```rust
// Create FTS5 virtual table with triggers
pub fn setup_fts(&self) -> Result<()> {
    self.conn.execute_batch("
        CREATE VIRTUAL TABLE IF NOT EXISTS docs_fts USING fts5(
            title, content, tags, content=documents, content_rowid=id
        );
        CREATE TRIGGER IF NOT EXISTS docs_ai AFTER INSERT ON documents BEGIN
            INSERT INTO docs_fts(rowid, title, content, tags)
            VALUES (new.id, new.title, new.content, new.tags);
        END;
    ")?;
    Ok(())
}

// Search with highlighting
pub fn search_documents(&self, query: &str) -> Result<Vec<Document>> {
    let mut stmt = self.conn.prepare(
        "SELECT d.*, highlight(docs_fts, 1, '<mark>', '</mark>') as snippet
         FROM documents d JOIN docs_fts ON d.id = docs_fts.rowid
         WHERE docs_fts MATCH ?1 ORDER BY rank"
    )?;
    stmt.query_map([query], |row| Ok(Document { /* ... */ }))?.collect()
}
```

---

## 5. Security Standards

### 5.1 Key Vulnerabilities

**Mitigation**: Update to SQLite 3.44.0+ and always use parameterized queries.

### 5.2 OWASP Mapping

| OWASP Category | Risk | Key Controls |
|----------------|------|--------------|
| A03 - Injection | Critical | Parameterized queries, input validation |
| A04 - Insecure Design | Medium | Schema constraints, foreign keys |
| A05 - Misconfiguration | Medium | Secure PRAGMAs, file permissions (600) |

### 5.3 SQL Injection Prevention

**Critical Rules** (see `references/security-examples.md`):
1. NEVER use string formatting for SQL queries
2. ALWAYS use `?` positional or `:name` named parameters
3. Whitelist column/table names for dynamic queries

```rust
// Dynamic column selection - SAFE approach
pub fn get_user_fields(&self, user_id: i64, fields: &[&str]) -> Result<HashMap<String, String>> {
    const ALLOWED: &[&str] = &["id", "name", "email", "created_at"];
    let safe_fields: Vec<&str> = fields.iter()
        .filter(|f| ALLOWED.contains(f)).copied().collect();
    if safe_fields.is_empty() { return Err(rusqlite::Error::InvalidQuery); }
    let query = format!("SELECT {} FROM users WHERE id = ?1", safe_fields.join(", "));
    let mut stmt = self.conn.prepare(&query)?;
    // ...
}
```

---

## 6. Testing Standards

### 6.1 Rust Testing Pattern

```rust
#[cfg(test)]
mod tests {
    use super::*;
    use rusqlite::Connection;

    fn setup_test_db() -> Database {
        let conn = Connection::open_in_memory().unwrap();
        let db = Database { conn };
        db.run_migrations().unwrap();
        db
    }

    #[test]
    fn test_sql_injection_prevented() {
        let db = setup_test_db();
        let result = db.search_users("'; DROP TABLE users; --", "active");
        assert!(result.is_ok());
        assert!(db.get_user_by_id(1).is_ok()); // Table still exists
    }
}
```

---

## 7. Implementation Workflow (TDD)

### Step 1: Write Failing Test First

```python
# tests/test_user_repository.py
import pytest
import sqlite3

@pytest.fixture
def db():
    """In-memory SQLite for fast testing."""
    conn = sqlite3.connect(":memory:")
    conn.row_factory = sqlite3.Row
    conn.execute("PRAGMA foreign_keys = ON")
    yield conn
    conn.close()

class TestUserRepository:
    def test_create_user_returns_id(self, db):
        repo = UserRepository(db)
        repo.initialize_schema()
        user_id = repo.create_user("[email protected]", "Test User")
        assert user_id > 0

    def test_sql_injection_prevented(self, db):
        repo = UserRepository(db)
        repo.initialize_schema()
        malicious = "'; DROP TABLE users; --"
        user_id = repo.create_user(malicious, "Hacker")
        assert repo.get_by_id(user_id)["email"] == malicious
```

### Step 2: Implement Minimum Code to Pass

```python
# app/repositories/user.py
class UserRepository:
    def __init__(self, conn):
        self.conn = conn

    def initialize_schema(self):
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS users (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                email TEXT NOT NULL UNIQUE,
                name TEXT NOT NULL
            )""")
        self.conn.commit()

    def create_user(self, email: str, name: str) -> int:
        cursor = self.conn.execute(
            "INSERT INTO users (email, name) VALUES (?, ?)", (email, name))
        self.conn.commit()
        return cursor.lastrowid

    def get_by_id(self, user_id: int):
        return self.conn.execute(
            "SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
```

### Step 3: Run Verification

```bash
pytest tests/test_*_repository.py -v --cov=app/repositories
```

---

## 7.1 Performance Patterns

### Pattern 1: WAL Mode

```python
# Good: Enable WAL for concurrent read/write
conn.execute("PRAGMA journal_mode = WAL")
conn.execute("PRAGMA synchronous = NORMAL")
conn.execute("PRAGMA cache_size = -64000")  # 64MB

# Bad: Default DELETE mode blocks reads during writes
```

### Pattern 2: Batch Inserts

```python
# Good: Single transaction for batch
conn.executemany("INSERT INTO items (name) VALUES (?)", records)
conn.commit()

# Bad: Commit per row (100x slower)
for r in records:
    conn.execute("INSERT INTO items (name) VALUES (?)", (r,))
    conn.commit()
```

### Pattern 3: Connection Pooling

```python
# Good: Reuse connections
from queue import Queue
class ConnectionPool:
    def __init__(self, db_path, size=5):
        self.pool = Queue(size)
        for _ in range(size):
            conn = sqlite3.connect(db_path, check_same_thread=False)
            conn.execute("PRAGMA journal_mode = WAL")
            self.pool.put(conn)

# Bad: New connection per query
conn = sqlite3.connect(db_path)  # Expensive!
```

### Pattern 4: Index Optimization

```python
# Good: Covering and partial indexes
conn.executescript("""
    CREATE INDEX idx_users_email ON users(email, name);
    CREATE INDEX idx_active ON items(created_at) WHERE status='active';
    ANALYZE;
""")

# Bad: Full table scan on unindexed columns
```

### Pattern 5: VACUUM Scheduling

```python
# Good: Maintenance during idle time
def nightly_maintenance(conn):
    conn.execute("PRAGMA optimize")
    freelist = conn.execute("PRAGMA freelist_count").fetchone()[0]
    if freelist > 1000:
        conn.execute("VACUUM")

# Bad: VACUUM during peak usage or never
```

---

## 8. Common Mistakes

| Mistake | Wrong | Correct |
|---------|-------|---------|
| SQL Injection | `format!("...WHERE name = '{}'", input)` | `"...WHERE name = ?1"` with params |
| No Transaction | Separate execute calls | Wrap in `transaction()` + `commit()` |
| No Foreign Keys | Default connection | `PRAGMA foreign_keys = ON` |
| LIKE for Search | `LIKE '%term%'` | FTS5 `MATCH 'term'` |

---

## 13. Pre-Implementation Checklist

### Phase 1: Before Writing Code

- [ ] **Tests written first** - Create failing tests for new database operations
- [ ] **Schema designed** - Document table structure, constraints, indexes
- [ ] **Security reviewed** - Identify all user inputs that reach database
- [ ] **Performance targets set** - Define query time limits and batch sizes
- [ ] **Reference files read** - Load `references/security-examples.md` if handling user input

### Phase 2: During Implementation

- [ ] **Parameterized queries only** - Never concatenate user input into SQL
- [ ] **Dynamic names whitelisted** - Column/table names from approved list only
- [ ] **Transactions for related ops** - Wrap multi-step operations in transactions
- [ ] **Foreign keys enabled** - `PRAGMA foreign_keys = ON` at connection
- [ ] **WAL mode configured** - For concurrent read/write access
- [ ] **Indexes created** - On columns used in WHERE, JOIN, ORDER BY
- [ ] **Batch operations used** - `executemany()` for multiple inserts
- [ ] **Error handling secure** - No SQL details in user-facing errors

### Phase 3: Before Committing

- [ ] **All tests pass** - Run `pytest tests/test_*_repository.py -v`
- [ ] **SQL injection test exists** - Verify malicious input is safely handled
- [ ] **Performance verified** - EXPLAIN QUERY PLAN shows index usage
- [ ] **Migrations tested** - Rollback works correctly
- [ ] **Schema version updated** - Migration tracking in place
- [ ] **Database permissions set** - File mode 600 for production
- [ ] **Backup strategy documented** - Recovery procedure verified
- [ ] **VACUUM scheduled** - Maintenance plan for database growth

---

## 14. Summary

Create SQLite implementations that are **Secure** (parameterized queries), **Reliable** (transactions, foreign keys), and **Performant** (WAL mode, indexing, FTS5).

**Security Reminder**: NEVER concatenate user input into SQL. ALWAYS use parameterized queries.

© JARVIS AI Assistant · Unlicense · martinholovsky/claude-skills-generator

Similar skills

All skills for Data and databases →

From the same set15

windows-ui-automation A skill for automating Windows apps through UI Automation and Win32 APIs: element discovery, input simulation, process interaction. Risk level HIGH. CX Codex +1 1,300installs gsap A skill that teaches the agent GSAP animations: HUD panel entrances, status indicator transitions, scroll effects. Useful when the UI is built on Vue. CX Codex +1 1,300installs cicd-expert A skill for building CI/CD: GitHub Actions, GitLab CI, Jenkins, security gates and ArgoCD GitOps. Useful when a pipeline needs writing or fixing. CX Codex +1 645installs webgl A WebGL skill for custom shaders and visual effects: holograms, post-processing, particle systems. Useful when you're building a 3D interface. CX Codex +1 555installs tailwindcss A skill that teaches AI agents to style the JARVIS assistant UI with Tailwind utility classes: dark HUD theme, responsive grids, animations, and tests before implementation. CX Codex +1 498installs rabbitmq-expert A skill that turns your AI agent into a RabbitMQ admin and developer: exchange patterns, clustering, high availability, production monitoring. Useful when you design queues or debug a cluster. CX Codex +1 491installs celery-expert Guides background job design on Celery: retries, chains, groups, chords, Celery Beat schedules and Flower monitoring. CX Codex +1 454installs wake-word-detection A skill for building wake word detection on openWakeWord: local audio monitoring, no cloud calls. Useful when you are wiring up a JARVIS-style assistant. CX Codex +1 404installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor