From 607e53b04ef2c0d9ead4a79540828bc9a4fb25cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 21 Mar 2021 13:54:33 +0100 Subject: [PATCH] Add an "In a glance" usage section --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 495948d67..66b9336a8 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,45 @@ Companion libraries that enhance and extend GRDB: ## Usage
+ In a glance + +**5 easy steps from nothing to the first fetch of your precious data:** + +```swift +import GRDB + +// 1. Open a database connection +let dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite") + +// 2. Define the database schema +try dbQueue.write { db in + try db.create(table: "player") { t in + t.autoIncrementedPrimaryKey("id") + t.column("name", .text).notNull() + t.column("score", .integer).notNull() + } +} + +// 3. Define a record type +struct Player: Codable, FetchableRecord, PersistableRecord { + var id: Int64 + var name: String + var score: Int +} + +// 4. Insert values +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) +} + +// 5. Fetch values +let players = try dbQueue.read(Player.fetchAll) // [Player] +``` + +
+ +
Connect to an SQLite database ```swift @@ -102,7 +141,7 @@ See [Database Connections](#database-connections)
- Execute SQL statements + Execute raw SQL statements ```swift try dbQueue.write { db in @@ -135,7 +174,7 @@ See [Executing Updates](#executing-updates)
- Fetch database rows and values + Fetch raw database rows and values ```swift try dbQueue.read { db in