Learn R Programming

etl (version 0.4.1)

dbRunScript: Execute an SQL script

Description

Execute an SQL script

Usage

dbRunScript(conn, script, echo = FALSE, ...)

Value

a list of results from dbExecute for each of the individual SQL statements in script.

Arguments

conn

a DBIConnection-class object

script

Either a filename pointing to an SQL script or a character vector of length 1 containing SQL.

echo

print the SQL commands to the output?

...

arguments passed to dbExecute

Details

The SQL script file must be ; delimited.

Examples

Run this code
sql <- "SHOW TABLES; SELECT 1+1 as Two;"
sql2 <- system.file("sql", "mtcars.mysql", package = "etl")
sql3 <- "SELECT * FROM user WHERE user = 'mysql';SELECT * FROM user WHERE 't' = 't';"

if (require(RSQLite)) {
  con <- dbConnect(RSQLite::SQLite())
  dbRunScript(con, "SELECT 1+1 as Two; VACUUM; ANALYZE;")
}
if (FALSE) {
if (require(RMySQL)) {
 con <- dbConnect(RMySQL::MySQL(), default.file = path.expand("~/.my.cnf"),
   group = "client", user = NULL, password = NULL, dbname = "mysql", host = "127.0.0.1")
 dbRunScript(con, script = sql)
 dbRunScript(con, script = sql2)
 dbRunScript(con, script = sql3)
 dbDisconnect(con)
}
}

Run the code above in your browser using DataLab