diff --git a/README.md b/README.md index b5da12fb4..ce0c83afa 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,8 @@ More than a set of tools that leverage SQLite abilities, GRDB is also: ## Usage -Open a [connection](#database-connections) to the database: +
+ Connect to an SQLite database ```swift import GRDB @@ -84,8 +85,13 @@ let dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite") // Enhanced multithreading based on SQLite's WAL mode let dbPool = try DatabasePool(path: "/path/to/database.sqlite") ``` + +See [Database Connections](#database-connections). -[Execute SQL statements](#executing-updates): +
+ +
+ Execute SQL statements ```swift try dbQueue.write { db in @@ -107,10 +113,16 @@ try dbQueue.write { db in } ``` -[Fetch database rows and values](#fetch-queries): +See [Executing Updates](#executing-updates). + +
+ +
+ Fetch database rows and values ```swift try dbQueue.read { db in + // Fetch database rows let rows = try Row.fetchCursor(db, "SELECT * FROM places") while let row = try rows.next() { let title: String = row["title"] @@ -119,18 +131,23 @@ try dbQueue.read { db in latitude: row["latitude"], longitude: row["longitude"]) } - + + // Fetch values let placeCount = try Int.fetchOne(db, "SELECT COUNT(*) FROM places")! // Int let placeTitles = try String.fetchAll(db, "SELECT title FROM places") // [String] } -// Extraction let placeCount = try dbQueue.read { db in try Int.fetchOne(db, "SELECT COUNT(*) FROM places")! } ``` -Insert and fetch [records](#records): +See [Fetch Queries](#fetch-queries) + +
+ +
+ Store custom models aka "records" ```swift struct Place { @@ -144,6 +161,15 @@ struct Place { // provide fetching and persistence methods. try dbQueue.write { db in + // Create database table + try db.create(table: "places") { t in + t.column("id", .integer).primaryKey() + t.column("title", .text).notNull() + t.column("favorite", .boolean).notNull().defaults(to: false) + t.column("longitude", .double).notNull() + t.column("latitude", .double).notNull() + } + var berlin = Place( id: nil, title: "Berlin", @@ -155,40 +181,42 @@ try dbQueue.write { db in berlin.isFavorite = true try berlin.update(db) - - // Fetch [Place] from SQL - let places = try Place.fetchAll(db, "SELECT * FROM places") } ``` -Avoid SQL with the [query interface](#the-query-interface): +See [Records](#records). + +
+ +
+ Fetch records and values with the Swift query interface ```swift try dbQueue.write { db in - try db.create(table: "places") { t in - t.autoIncrementedPrimaryKey("id") - t.column("title", .text).notNull() - t.column("favorite", .boolean).notNull().defaults(to: false) - t.column("longitude", .double).notNull() - t.column("latitude", .double).notNull() - } - // Place? let paris = try Place.fetchOne(db, key: 1) // Place? - let titleColumn = Column("title") - let berlin = try Place.filter(titleColumn == "Berlin").fetchOne(db) + let berlin = try Place.filter(Column("title") == "Berlin").fetchOne(db) // [Place] - let favoriteColumn = Column("favorite") let favoritePlaces = try Place - .filter(favoriteColumn) - .order(titleColumn) + .filter(Column("favorite")) + .order(Column("title")) .fetchAll(db) + + // Int + let favoriteCount = try Place.filter(Column("favorite")).fetchCount(db) + + // SQL is always welcome + let places = try Place.fetchAll(db, "SELECT * FROM pointOfInterests") } ``` +See the [Query Interface](#the-query-interface). + +
+ Documentation =============