0

I have a data frame DF_1 in the MYSQL DB , Now i want to add DF_2 from Rstudio to DF_1 which is there in DB. for this I am using code as below

DF_1 <- data.frame("SN" = 1:3, "Age" = c(21,15,20), "Name" = c("John","Dora","Mike"))

DF_2 <- data.frame("SN" = 1:3, "Age" = c(21,15,30), "Name" = c("John","Dora","Clark"))

 dbWriteTable(connection, "df_1", DF_2,row.names=F,append=T)

But the above code writes duplicates in DB

Any modifications in the above code would be helpful.

Thanks in advance

sasir
  • 167
  • 10
  • You may check [here](https://stackoverflow.com/questions/2630440/how-to-delete-duplicates-on-a-mysql-table) – akrun Nov 10 '17 at 05:36
  • Thanks Akrun for your quick response.. Any modifications in the R code so that i can avoid duplicates before writing the data frame to DB. – sasir Nov 10 '17 at 05:39
  • 1
    I am assuming the DF_1 is not already created as object in the R env. If it is, then `unique(rbind(DF_1, DF_2))` would do – akrun Nov 10 '17 at 05:42
  • Also check with `dbSendQuery` – akrun Nov 10 '17 at 05:43
  • Its(DF_1) already created as data frame and stored in DB, DF_2 is next set of data that i am trying to append to df_1 there in MYSQL DB – sasir Nov 10 '17 at 05:44
  • 1
    You may also check [here](https://stackoverflow.com/questions/39154234/r-and-mysql-checking-if-a-row-exists-in-a-table-before-writing-to-it-using-dbw) – akrun Nov 10 '17 at 05:45
  • 1
    Thank you very much Akrun , I have learn a new thing..I did it "df_1 <- dbGetQuery(connection, "SELECT * FROM df_1")" .."df_1 <- rbindlist(list(df_1, DF_2), fill = TRUE)"...."df_1 <- unique(df_1)"... – sasir Nov 10 '17 at 06:05
  • finally dbWriteTable(connection, value = df_1, name = "df_1", overwrite = TRUE, row.names = FALSE) – sasir Nov 10 '17 at 06:05

0 Answers0