如果 R 中不存在,则将新行/数据追加到数据库中

问题描述 投票:0回答:1

我想在 DWH 中创建一个表来构建有关某些内容的历史信息,该表从实时数据库获取数据,如果发生这种情况,我的表中的更新将仅在我表的列中,我想将这个新的原始数据附加到我在 DWH 中的数据。

# write the data in the DHW for the first time and then append the new data:

dbWriteTable(DWH, Id(schema = "public", table = "test_df"), customer, row.names=FALSE, overwrite=TRUE)


# i use this method to append the data but it append all data even if it is there:
dbAppendTable( DWH, "test_df", CUSTOMER,overwrite=TRUE)

我想要的只是将新数据附加到 DWH 中的表中,而不是所有数据。 有办法做到这一点吗??

r database append dbi
1个回答
0
投票
library(dplyr)
# write the data in the DHW for the first time and then append the new data:
dbWriteTable(DWH, Id(schema = "public", table = "test_df"), old_data_df, row.names = FALSE, overwrite = TRUE)

# use dplyr::anti_join to retain only the rows in new_data_df that are not in old_data_df 
to_insert_df <- anti_join(new_data_df, old_data_df)

# i use this method to append the data but it append all data even if it is there:
dbAppendTable(DWH, "test_df", to_insert_df, overwrite = FALSE)
© www.soinside.com 2019 - 2024. All rights reserved.