DBI (version 0.5-1)

dbBind: Bind values to a parameterised/prepared statement

Description

The dbSendQuery function can be called with queries that contain placeholders for values. This function binds these placeholders to actual values, and is intended to be called on the result of dbSendQuery before calling dbFetch.

Usage

dbBind(res, params, ...)

Arguments

res
An object inheriting from DBIResult.
params
A list of bindings
...
Other arguments passed on to methods.

Details

Parametrised or prepared statements are executed as follows:

  1. Call dbSendQuery with a query that contains placeholders, store the returned DBIResult object in a variable. Currently, the syntax for the placeholders is backend-specific, e.g., ?, $, $name and :name. Mixing placeholders (in particular, named and unnamed ones) is not recommended.
  2. Call dbBind on the DBIResult object with a list that specifies actual values for the placeholders. The list must be named or unnamed, depending on the kind of placeholders used. Named values are matched to named paramters, unnamed values are matched by position.
  3. Call dbFetch on the same DBIResult object.
  4. Repeat 2. and 3. as necessary.
  5. Close the result set via dbClearResult.

See Also

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

Examples

Run this code
## Not run: 
# con <- dbConnect(RSQLite::SQLite(), ":memory:")
# 
# dbWriteTable(con, "iris", iris)
# iris_result <- dbSendQuery(con, "SELECT * FROM iris WHERE [Petal.Width] > ?")
# dbBind(iris_result, list(2.3))
# dbFetch(iris_result)
# dbBind(iris_result, list(3))
# dbFetch(iris_result)
# 
# dbClearResult(iris_result)
# dbDisconnect(con)
# ## End(Not run)

Run the code above in your browser using DataCamp Workspace