GRDB.swift
A toolkit for SQLite databases, with a focus on application development
What is GRDB.swift?
A toolkit for SQLite databases, with a focus on application development
Overview of GRDB.swift
What it is
GRDB.swift is a toolkit for SQLite databases, focused on application development. It provides tools for SQL generation, database observation, robust concurrency, and migrations, allowing developers to work with SQLite databases in Swift.
Key features
- SQL generation: Enhance application models with persistence and fetching methods, reducing the need for raw SQL.
- Database observation: Get notifications when database values are modified.
- Robust concurrency: Multi-threaded applications can efficiently use databases, including WAL databases that support concurrent reads and writes.
- Migrations: Evolve the database schema as new versions of the application are shipped.
- Leverage SQLite skills: Supports advanced SQLite features for developers who need them.
Use cases
GRDB.swift is suitable for saving permanent data into SQLite databases in iOS, macOS, tvOS, and watchOS applications. It can be used for tasks such as defining database schemas, performing CRUD operations, querying with a Swift query interface, and observing database changes.
Getting started
swiftimport GRDB// 1. Open a database connectionlet dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite")// 2. Define the database schematry dbQueue.write { db in try db.create(table: "player") { t in t.primaryKey("id", .text) t.column("name", .text).notNull() t.column("score", .integer).notNull() }}// 3. Define a record typestruct Player: Codable, Identifiable, FetchableRecord, PersistableRecord { var id: String var name: String var score: Int enum Columns { static let name = Column(CodingKeys.name) static let score = Column(CodingKeys.score) }}// 4. Write and read in the databasetry dbQueue.write { db in try Player(id: "1", name: "Arthur", score: 100).insert(db) try Player(id: "2", name: "Barbara", score: 1000).insert(db)}try dbQueue.read { db in let player = try Player.find(db, id: "1") let bestPlayers = try Player .order(score.desc) .limit(10) .fetchAll(db)}
License and pricing
GRDB.swift is open source under the MIT license. No pricing information is provided.
這是什麼
GRDB.swift 是一套專注於應用程式開發的 SQLite 資料庫工具組。它提供 SQL 生成、資料庫觀察、穩健的並行處理以及遷移工具,讓開發者能夠在 Swift 中操作 SQLite 資料庫。
主要功能
- SQL 生成:透過持久化與擷取方法強化應用程式模型,減少直接使用 SQL 的需求。
- 資料庫觀察:在資料庫值變更時接收通知。
- 穩健的並行處理:多執行緒應用程式能有效使用資料庫,包括支援並行讀寫的 WAL 資料庫。
- 遷移:隨著應用程式新版本的發布,演進資料庫結構。
- 善用 SQLite 技能:支援需要進階 SQLite 功能的開發者。
使用情境
GRDB.swift 適用於在 iOS、macOS、tvOS 和 watchOS 應用程式中將永久資料儲存至 SQLite 資料庫。可用於定義資料庫結構、執行 CRUD 操作、使用 Swift 查詢介面進行查詢,以及觀察資料庫變更。
快速開始
swiftimport GRDB// 1. 開啟資料庫連線let dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite")// 2. 定義資料庫結構try dbQueue.write { db in try db.create(table: "player") { t in t.primaryKey("id", .text) t.column("name", .text).notNull() t.column("score", .integer).notNull() }}// 3. 定義記錄類型struct Player: Codable, Identifiable, FetchableRecord, PersistableRecord { var id: String var name: String var score: Int enum Columns { static let name = Column(CodingKeys.name) static let score = Column(CodingKeys.score) }}// 4. 在資料庫中寫入與讀取try dbQueue.write { db in try Player(id: "1", name: "Arthur", score: 100).insert(db) try Player(id: "2", name: "Barbara", score: 1000).insert(db)}try dbQueue.read { db in let player = try Player.find(db, id: "1") let bestPlayers = try Player .order(score.desc) .limit(10) .fetchAll(db)}
授權與價格
GRDB.swift 是開放原始碼軟體,採用 MIT 授權條款。未提供價格資訊。
Based on the project's public README and repository metadata.