简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM