2

I have many table that I am saving to a MariaDb in AWS RDS. I can manually save the tables. However I want to create a loop to do and I can't figure out the syntax on the dbWriteTable command. library(RMySQL)

dbWriteTable(con, "Account" , Account, overwrite = T)
dbWriteTable(con, "Campaign",  Campaign, overwrite = T)
dbWriteTable(con, "Contact" , Contact, overwrite = T)
dbWriteTable(con, "User", User, overwrite =T)

Instead I would like to do it in a loop.

nm = c("Account", "Campaign", "Contact",  "User")

for (i in 1:length(nm)) {

  dbWriteTable(con,  nm[i], paste(nm[i]), overwrite = TRUE)
 }
Spruce Island
  • 425
  • 1
  • 4
  • 10

2 Answers2

3

Per comments above, using get0 instead of paste like so will work:

nm = c("Account", "Campaign", "Contact",  "User")

for (i in 1:length(nm)) {
    dbWriteTable(con,  nm[i], get0(nm[i]), overwrite = TRUE)
}
Zach
  • 1,103
  • 8
  • 11
0

Try this instead:

for (i in nm){
  dbWriteTable(con,  i, paste(i), overwrite = TRUE)
}

You do not need to extract the i from the original list with brackets, because i represents directly that object in the list. Essentially you are saying for each instance in nm, write to the db table named i (which will be one of the words in your list) and paste the value of i as the name.

sconfluentus
  • 4,693
  • 1
  • 21
  • 40
  • I get the error. Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'Account': No such file or directory. Its like the data.frame can't be found.. I have tried so many different things... – Spruce Island Oct 28 '16 at 22:28
  • Sorry it was of no help. Because I did not have the files to over-write I could not test. I ran the loop with prints and returns. But that sounds like a working directory directory issue...maybe `setwd(" your/directory/here")` – sconfluentus Oct 29 '16 at 02:15