在考虑使用 dplyr 将数据添加到表中时,我看到了/sf/answers/1874936101/,但文档说db_insert_table()已弃用。
?db_insert_into()
...
db_create_table() and db_insert_into() have been deprecated in favour of db_write_table().
...
Run Code Online (Sandbox Code Playgroud)
我尝试改用未弃用的选项db_write_table(),但无论有没有append=选项,它都会失败:
require(dplyr)
my_db <- src_sqlite( "my_db.sqlite3", create = TRUE) # create src
copy_to( my_db, iris, "my_table", temporary = FALSE) # create table
newdf = iris # create new data
db_write_table( con = my_db$con, table = "my_table", values = newdf) # insert into
# Error: Table `my_table` exists in database, and both overwrite and append are FALSE
db_write_table( con = my_db$con, table = "my_table", values = newdf,append=True) # insert into
# Error: Table `my_table` exists in database, and both overwrite and append are FALSE
Run Code Online (Sandbox Code Playgroud)
应该能够使用 附加数据db_write_table()吗?
不,您不应该使用 db_write_table() 而不是 db_insert_table(),因为它不能跨后端泛化。
并且您不应该使用 tidyverse 版本而不是相关的 DBI::: 版本,因为 tidyverse 辅助函数仅供内部使用,并且设计得不够强大以供用户使用。请参阅https://github.com/tidyverse/dplyr/issues/3120#issuecomment-339034612的讨论:
实际上,我认为您根本不应该使用这些功能。尽管有这样的帖子,但这些不是面向用户的功能。您应该直接调用 DBI 函数。
——Hadley Wickham,软件包作者。