Prasinus Albus
Prasinus Albus

Reputation: 416

r-mysql: use r variable to fetch a column from database

Can I pass an r variable into a function for selecting a column from the database so that different columns are selected every time the variable in r changes:

father <- 'father'
myfun(father)

Function:

myfun <- function (parent)
{
query <-  paste("SELECT '$parent' from table1 where EXTRACT(YEAR FROM dob) 
between '",date1,"' and '",date2,"'",sep='')

connect1 <<- dbGetQuery (con, query )
connect1
}

Upvotes: 0

Views: 247

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520888

It is not possible to have a table column be a parameter in a prepared statement. But I don't see anything stopping you from trying something along the lines of the following:

parent <- "father"
query <- paste0("SELECT ", parent, " FROM table1 WHERE YEAR(dob) BETWEEN '", date1, "' AND '", date2, "'", sep='')

Upvotes: 1

Related Questions