I am using RMySQL and DBI for the connection between R and MySQL
library(RMySQL)
library(DBI, quietly = TRUE)
Everything is working fine for one command, such as
sql = "select * from clients"
con <- dbConnect(MySQL(),user=user, password=password, dbname=dbname, host=host)
rs <- dbSendQuery(con, sql)
data <- fetch(rs, n=-1)
huh <- dbHasCompleted(rs)
dbClearResult(rs)
on.exit(dbDisconnect(con))
However, when I want to execute multiple commands with ";" between them (such as to set a parameter), it returns error. For example
sql = "SET @LAST_TEN_DAY = DATE_ADD(NOW(), INTERVAL -10 DAY); select * from clients where date > @LAST_TEN_DAY"
con <- dbConnect(MySQL(),user=user, password=password, dbname=dbname, host=host)
rs <- dbSendQuery(con, sql)
data <- fetch(rs, n=-1)
huh <- dbHasCompleted(rs)
dbClearResult(rs)
on.exit(dbDisconnect(con))
Many thanks,
DATE_ADD(NOW(), INTERVAL, -10 DAY)
and then use the SQL querySELECT * FROM CLIENTS WHERE DATE> your variable value
– Tetragramdput(clients)
– Tetragram(list(name = structure(c(3L, 1L, 4L, 2L), .Label = c("Anna", "Ciaran", "Hannah", "Roisin"), class = "factor"), date = structure(c(3L, 4L, 2L, 1L), .Label = c("2015-12-12 00:00:00", "2016-01-22 00:00:00", "2016-02-12 00:00:00", "2016-03-08 00:00:00"), class = "factor"), age = c(27, 28, 22, 19), job = structure(c(1L, 1L, 2L, 1L ), .Label = c("data analyst", "operator"), class = "factor")), .Names = c("name", "date", "age", "job"), row.names = c(NA, -4L), class = "data.frame")
– Stinker