# 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. ## Install Save in your project root: # SQLite.swift — Type-Safe SQLite Layer for Swift ## Quick Use ```swift // SPM: .package(url: "https://github.com/stephencelis/SQLite.swift.git", from: "0.15.0") import SQLite let db = try Connection("app.sqlite3") let users = Table("users") let id = SQLite.Expression("id") let name = SQLite.Expression("name") try db.run(users.create { t in t.column(id, primaryKey: .autoincrement) t.column(name) }) try db.run(users.insert(name <- "Alice")) for user in try db.prepare(users) { print(user[name]) } ``` ## 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 - https://github.com/stephencelis/SQLite.swift - https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md --- Source: https://tokrepo.com/en/workflows/asset-a7380f5a Author: AI Open Source