Instance Methodgrdb 7.1.0GRDB

fetchCursor(_:)

Returns a cursor over fetched rows.

Row.swift:1924
func fetchCursor(_ db: Database) throws -> RowCursor

Parameters

db

A database connection.

Returns

A RowCursor over fetched rows.

Throws

A DatabaseError whenever an SQLite error occurs.

For example:

try dbQueue.read { db in
    let lastName = "O'Reilly"
    let request: SQLRequest<Row> = """
        SELECT * FROM player WHERE lastName = \(lastName)
        """
    let rows = try request.fetchCursor(db)
    while let row = try rows.next() {
        let id: Int64 = row["id"]
        let name: String = row["name"]
    }
}

Fetched rows are reused during the cursor iteration: don’t turn a row cursor into an array with Array(rows) since you would not get the distinct rows you expect. Use fetchAll(_:) instead.

For the same reason, make sure you make a copy whenever you extract a row for later use: row.copy().

The returned cursor is valid only during the remaining execution of the database access. Do not store or return the cursor for later use.

If the database is modified during the cursor iteration, the remaining elements are undefined.