GRDB stories

Gwendal Roué
1 min readMar 30, 2016

--

The little things that make the SQLite library GRDB.swift different.

What purpose is storing data in a database, if not getting it back?

Database rows are not always what you want. Counting? You need an Integer. Loading books? You need an array of Book objects.

Fetching with GRDB is simple: pick the fetched type, and whether you want a single value, an array of values, or a cursor of values (use cursors when you don’t need to keep all results in memory):

Type.fetchOne(...)    // Type?
Type.fetchAll(...) // [Type]
Type.fetchCursor(...) // DatabaseCursor<Type>

Any type that can be fetched is OK: database Row, your custom Records, and also Int, String, Date and other values:

Int.fetchOne(db, "SELECT COUNT(*) FROM people") // Int?
String.fetchAll(db, "SELECT title FROM books") // [String]
Book.fetchCursor(db, "SELECT * FROM books") // DatabaseCursor<Book>

Little game: what are the types of those values?

Country.fetchOne(db, key: "FR")
Book.filter(authorId == 1).order(title).fetchAll(db)
Double.fetchOne(db, Country.select(max(area)))

https://github.com/groue/GRDB.swift

--

--