R: Fetch records from a previously executed query.
dbFetch
R Documentation
Fetch records from a previously executed query.
Description
Fetch the next n elements (rows) from the result set and return them
as a data.frame.
Usage
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 should provide methods for both fetch and
dbFetch until fetch is deprecated in 2015.
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: dbClearResult,
dbColumnInfo, dbGetRowCount,
dbGetRowsAffected,
dbGetStatement,
dbHasCompleted
Examples
if (!require("RSQLite")) {
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "mtcars", mtcars)
# Fetch all results
res <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
dbFetch(res)
dbClearResult(res)
# Fetch in chunks
res <- dbSendQuery(con, "SELECT * FROM mtcars")
while (!dbHasCompleted(res)) {
chunk <- fetch(res, 10)
print(nrow(chunk))
}
dbClearResult(res)
dbDisconnect(con)
}
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(DBI)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/DBI/dbFetch.Rd_%03d_medium.png", width=480, height=480)
> ### Name: dbFetch
> ### Title: Fetch records from a previously executed query.
> ### Aliases: dbFetch dbFetch,DBIResult-method fetch
>
> ### ** Examples
>
> if (!require("RSQLite")) {
+ con <- dbConnect(RSQLite::SQLite(), ":memory:")
+ dbWriteTable(con, "mtcars", mtcars)
+
+ # Fetch all results
+ res <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
+ dbFetch(res)
+ dbClearResult(res)
+
+ # Fetch in chunks
+ res <- dbSendQuery(con, "SELECT * FROM mtcars")
+ while (!dbHasCompleted(res)) {
+ chunk <- fetch(res, 10)
+ print(nrow(chunk))
+ }
+ dbClearResult(res)
+ dbDisconnect(con)
+ }
Loading required package: RSQLite
>
>
>
>
>
> dev.off()
null device
1
>