Fetch records from a previously executed query

Share:

Description

Fetch the next n elements (rows) from the result set and return them as a data.frame.

Usage

1
2
3
dbFetch(res, n = -1, ...)

fetch(res, n = -1, ...)

Arguments

res

An object inheriting from DBIResult.

n

maximum number of records to retrieve per fetch. Use n = -1 to retrieve all pending records. Some implementations may recognize other special values.

...

Other arguments passed on to methods.

Details

fetch is provided for compatibility with older DBI clients - for all new code you are strongly encouraged to use dbFetch. The default method for dbFetch calls fetch so that it is compatible with existing code. Implementors are free to provide methods for dbFetch only.

Value

a data.frame with as many rows as records were fetched and as many columns as fields in the result set.

See Also

close the result set with dbClearResult as soon as you finish retrieving the records you want.

Other DBIResult generics: DBIResult-class, SQL, dbBind, dbClearResult, dbColumnInfo, dbGetInfo, dbGetRowCount, dbGetRowsAffected, dbGetStatement, dbHasCompleted, dbIsValid

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
con <- dbConnect(RSQLite::SQLite(), ":memory:")

dbWriteTable(con, "mtcars", mtcars)

# Fetch all results
rs <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
dbFetch(rs)
dbClearResult(rs)

# Fetch in chunks
rs <- dbSendQuery(con, "SELECT * FROM mtcars")
while (!dbHasCompleted(rs)) {
  chunk <- dbFetch(rs, 10)
  print(nrow(chunk))
}

dbClearResult(rs)
dbDisconnect(con)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.