tw123789
tw123789

Reputation: 55

How to do dbGetQuery for loop in R

i have three variables a,b,c (Actually more than 300 variables in my case)

t<-c(a,b,d)

a<-dbGetQuery(con, "SELECT * FROM a")
b<-dbGetQuery(con, "SELECT * FROM b")
d<-dbGetQuery(con, "SELECT * FROM d")

How can I make a loop to request data from MySQL in R? The existing question does not have the explanation on how to write it into the variable names. I need a,b,c in my environment.

Upvotes: 0

Views: 1555

Answers (1)

zx8754
zx8754

Reputation: 56119

Not tested, but something as below should work.

myTables <- c("a","b","c")

res <- lapply(myTables,
              function(myTable){
                sqlStatement <- paste("select * from",myTable)
                dbGetQuery(con, sqlStatement)
              })
names(res) <- myTables

Upvotes: 1

Related Questions