db.eval: Evaluate SQL statements

View source: R/db-eval.R

db.evalR Documentation

Evaluate SQL statements

Description

Evaluate SQL statements

Usage

db.eval(
  db,
  stmt,
  params,
  row_factory = "identity",
  simplify = getOption("DBPKG_SIMPLIFY_RETURN", FALSE)
)

Arguments

db

The database connection. An S4 object of class "database".

stmt

The SQL statement to evaluate. SQL parameters can be bound to the statement and are indicated by the '?' character.

params

A matrix, data.frame, or list of lists with parameters to bind the SQL stmt. Each row in the matrix or data.frame or each sublist in the list of lists corresponds to a single parameter set. The number of parameters in each parameter set should equal the number of '?' characters in the SQL statement. If multiple parameter sets are bound to the statement then the statement may not be a SELECT statement. For the common case when there is only a single parameter set it is permitted to pass an atomic vector or a single list (i.e., no sublists) with the parameter values to bind.

row_factory

The name of a function to apply to each row of the result set before it is returned.

Value

The result of a SELECT statement or NULL if the statement is not a SELECT. Results will be returned as a matrix (row_factory="identity") or data.frame (row_factory="data.frame") containing the rows from the result set, or a list, each element of which is the result of applying row_factory to the corresponding row in the result set. If row_factory="identity" subsets of the matrix will return lists rather than atomic vectors. This is necessary because elements of the matrix can hold any type of object that are not necessarily the same from one element to the next.

Examples

db = db.open()
db.eval(db, "CREATE TABLE foo(f1 TEXT)")
db.eval(db, "INSERT INTO foo VALUES (?)", list("hello"))
db.eval(db, "INSERT INTO foo VALUES (?)", c("world"))
db.eval(db, "INSERT INTO foo VALUES (?)", data.frame(letters))
db.eval(db, "INSERT INTO foo VALUES (?)", matrix("goodbye", 1, 1))
db.eval(db, "SELECT * FROM foo")
db.close(db)

blueraleigh/db documentation built on Feb. 25, 2024, 9:13 a.m.