The GRDB documentation says:
Once granted with a database connection, the execute
method executes the SQL statements that do not return any database row, such as CREATE TABLE, INSERT, DELETE, ALTER, etc.
So if you want to fetch rows, you picked the wrong method.
To fetch rows, you use a fetching method (which starts with fetch
), as documented in the Row Queries documentation chapter, that I encourage you to read.
For example:
let rows = try dbQueue.inDatabase { db in
try Row.fetchAll(db, "SELECT * FROM games")
}
// Now we're back to the main thread: use rows:
for row in rows {
let name: String = row.value(named: "name")
let year: Int = row.value(named: "year")
print(name)
print(year)
}
That's what it gives "without going through models".
Yet models are still handy:
let games = try dbQueue.inDatabase { db -> [Game] in
let rows = try Row.fetchAll(db, "SELECT * FROM games")
return rows.map { row in
Game(
name: row.value(named: "name"),
year: row.value(named: "year"))
}
}
// Now we're back to the main thread: use games
for game in games {
print(game.name)
print(game.year)
}
If you have your Game type (struct or class) adopt the RowConvertible protocol, you can even write:
let games = try dbQueue.inDatabase { db in
try Game.fetchAll(db, "SELECT * FROM games")
}
And if Game also adopts TableMapping protocol, you get:
let games = try dbQueue.inDatabase { try Game.fetchAll($0) }
See Record for the documentation of those protocols.