简体   繁体   English

r dbWriteTable循环

[英]r dbWriteTable in loop

I have many table that I am saving to a MariaDb in AWS RDS. 我有很多表要保存到AWS RDS中的MariaDb中。 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. 但是,我想创建一个循环来做,但无法弄清楚dbWriteTable命令的语法。 library(RMySQL) 库(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)
 }

Per comments above, using get0 instead of paste like so will work: 根据上面的评论,使用get0而不是像paste这样将起作用:

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

for (i in 1:length(nm)) {
    dbWriteTable(con,  nm[i], get0(nm[i]), overwrite = TRUE)
}

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. 您不需要使用括号将原始列表中的i提取出来,因为i直接表示列表中的该对象。 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. 本质上,您是针对nm中的每个实例说的,写入名为i的数据库表(这将是列表中的单词之一),然后将i的值粘贴为名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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