sendQuery | R Documentation |
This functions sends a query to a database and fetches the result.
sendQuery(db, query, ...)
## S4 method for signature 'CredentialsList,SingleQueryList'
sendQuery(db, query, ..., applyFun = lapply, simplify = TRUE)
## S4 method for signature 'CredentialsORCredentialsList,character'
sendQuery(db, query, ...)
## S4 method for signature 'Credentials,SingleQueryList'
sendQuery(db, query, ..., simplify = TRUE)
## S4 method for signature 'DBIConnection,SingleQuery'
sendQuery(db, query, ...)
## S4 method for signature 'MySQLConnection,SingleQuery'
sendQuery(db, query, ..., encoding = "utf8mb4")
db |
one in: |
query |
one in: |
... |
one in: |
applyFun |
(function) something like lapply or mclapply |
simplify |
(logical(1)) whether to simplify results. See details. |
encoding |
(character | NULL) the encoding used in a |
simplify
the default is to simplify results. If you send
multiple queries to one database it is tried to rbind the results - when
you have different column names this can be like a full join. If you send
one query to multiple databases it is tried to rbind the results. If you
send multiple queries to multiple databases, then first the results of the
same query are tried to be rbind, and if possible also the results of each
query. It is considered to be possible iff the names of all data frames
belonging to each query are the same.
one in:
simplify = TRUE
(list | data.frame)
simplify = FALSE
: (list) with data frames or nested list of data
frames
On error: (list) with 'try-catch' objects
## For an example database:
library("RSQLite")
con <- dbConnect(SQLite(), "example.db")
USArrests$State <- rownames(USArrests)
dbWriteTable(con, "USArrests", USArrests, row.names = FALSE)
dbDisconnect(con)
## Simple Query
cred <- Credentials(drv = SQLite, dbname = "example.db")
dat <- sendQuery(cred, "SELECT * FROM USArrests;")
## Multiple Similar Queries
queryFun <- function(state) {
paste0("SELECT * FROM USArrests WHERE State = '", state, "';")
}
sendQuery(cred, queryFun(dat$row_names))
## For the Paranoid
### be a bit more cautious with connections
dat <- try(sendQuery(
cred,
"SELECT * FROM USArrest;", # wrong name for illustration
tries = 2,
intSleep = 1
))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.