Learn R Programming

RSQLite (version 1.0.0)

query: Execute a SQL statement on a database connection

Description

To retrieve results a chunk at a time, use dbSendQuery, dbFetch, then ClearResult. Alternatively, if you want all the results (and they'll fit in memory) use dbGetQuery which sends, fetches and clears for you.

Usage

# S4 method for SQLiteConnection,character
dbSendQuery(conn, statement)

# S4 method for SQLiteConnection,character,data.frame dbSendPreparedQuery(conn, statement, bind.data)

# S4 method for SQLiteResult dbFetch(res, n = 0)

# S4 method for SQLiteResult fetch(res, n = 0)

# S4 method for SQLiteResult dbClearResult(res, ...)

# S4 method for SQLiteConnection dbClearResult(res, ...)

# S4 method for SQLiteConnection dbListResults(conn, ...)

# S4 method for SQLiteConnection,character dbGetQuery(conn, statement)

# S4 method for SQLiteConnection,character,data.frame dbGetPreparedQuery(conn, statement, bind.data)

Arguments

conn

an '>SQLiteConnection object.

statement

a character vector of length one specifying the SQL statement that should be executed. Only a single SQL statment should be provided.

bind.data

A data frame of data to be bound.

res

an '>SQLiteResult object.

n

maximum number of records to retrieve per fetch. Use -1 to retrieve all pending records; use 0 for to fetch the default number of rows as defined in SQLite

...

Unused. Needed for compatibility with generic.

Examples

Run this code
# NOT RUN {
con <- dbConnect(SQLite(), ":memory:")
dbWriteTable(con, "arrests", datasets::USArrests)

# Run query to get results as dataframe
dbGetQuery(con, "SELECT * FROM arrests limit 3")

# Send query to pull requests in batches
res <- dbSendQuery(con, "SELECT * FROM arrests")
data <- fetch(res, n = 2)
data
dbHasCompleted(res)

dbListResults(con)
dbClearResult(res)

# Use dbSendPreparedQuery/dbGetPreparedQuery for "prepared" queries
dbGetPreparedQuery(con, "SELECT * FROM arrests WHERE Murder < ?",
   data.frame(x = 3))
dbGetPreparedQuery(con, "SELECT * FROM arrests WHERE Murder < (:x)",
   data.frame(x = 3))

dbDisconnect(con)
# }

Run the code above in your browser using DataLab