ConfigsJul 6, 2026·3 min read

SQLite.swift — Type-Safe SQLite Layer for Swift

A type-safe Swift-language wrapper over SQLite3 that provides compile-time checked queries with an expressive builder API.

Agent ready

Ready-to-run agent install

This asset can be installed after the agent chooses its runtime, checks the plan, and runs the matching command.

Native · 98/100Policy: allow
Agent surface
Any MCP/CLI agent
Kind
Skill
Install
Single
Trust
Trust: Established
Entrypoint
SQLite.swift Overview
Direct install command
npx -y tokrepo@latest install a7380f5a-7959-11f1-9bc6-00163e2b0d79 --target codex

Run after dry-run confirms the install plan.

Introduction

SQLite.swift provides a thin, type-safe Swift wrapper around SQLite3. It lets you build SQL queries using Swift expressions that the compiler checks at build time, catching column type mismatches and query errors before runtime. It works directly with the system SQLite library on Apple platforms.

What SQLite.swift Does

  • Provides a typed expression DSL for building SELECT, INSERT, UPDATE, DELETE queries
  • Catches column type mismatches at compile time through Swift generics
  • Manages SQLite connections with automatic statement finalization
  • Supports transactions, savepoints, and WAL journal mode
  • Offers both the type-safe query builder and raw SQL escape hatches

Architecture Overview

The library defines Expression types that carry both the SQL column name and the Swift type. Table objects combine these expressions into query builders that produce valid SQL strings. The Connection class wraps sqlite3 pointers and handles statement preparation, binding, and stepping. FTS4/FTS5 full-text search and R-Tree modules are supported through optional extensions.

Self-Hosting & Configuration

  • Add via SPM from stephencelis/SQLite.swift
  • Or via CocoaPods: pod 'SQLite.swift', '~> 0.15'
  • Uses the system-provided SQLite3 library; no bundled C code
  • Supports iOS 13+, macOS 10.15+, tvOS 13+, watchOS 6+, and Linux
  • Enable custom SQLite builds by pointing to your own module map

Key Features

  • Compile-time type checking of query expressions and column types
  • Full-text search via FTS4 and FTS5 virtual table support
  • Custom SQL function and collation registration
  • Codable integration for automatic model serialization to/from rows
  • Thread-safe connection pooling through serialized queue access

Comparison with Similar Tools

  • Core Data — Apple's ORM with iCloud sync; heavier abstraction, less SQL control
  • GRDB.swift — full-featured SQLite toolkit with record types and migrations; larger API surface
  • Realm — cross-platform mobile database; proprietary file format, not standard SQLite
  • FMDB — Objective-C SQLite wrapper; less idiomatic for modern Swift code
  • SwiftData — Apple's newest persistence framework; requires iOS 17+ and ties to the Apple ecosystem

FAQ

Q: Can I use raw SQL queries alongside the type-safe builder? A: Yes. Connection.execute() and Connection.prepare() accept raw SQL strings for cases the builder does not cover.

Q: Does SQLite.swift support migrations? A: It provides user_version tracking but no built-in migration runner. You manage schema changes via conditional DDL statements.

Q: Is it compatible with Swift concurrency (async/await)? A: The core API is synchronous. You can wrap calls in Task or actor isolation. GRDB.swift offers native async support if that is a priority.

Q: Can I use it on Linux? A: Yes, when linked against a system SQLite3 library. The SPM package supports Linux targets.

Sources

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets