英文:
Loading and/or attaching `package:DBI` and `package:odbc` et al.?
问题
我在DBI文档中找不到有关如何加载前端(即package:DBI
)和后端(例如package:odbc
)的明确指导。
文档中的大多数示例(但不是全部)将package:DBI
附加到搜索路径上,而不附加后端,例如:
library(DBI)
con <- dbConnect(odbc::odbc(), ...)
也有一次提交 偏向于这种方法。
为什么文档使用这种方法而不是例如:
# 仅附加后端
library(odbc)
con <- dbConnect(odbc(), ...)
# 在附加package:DBI之前附加后端
library(odbc)
library(DBI)
con <- dbConnect(odbc(), ...)
# 在附加package:DBI之后附加后端
library(DBI)
library(odbc)
con <- dbConnect(odbc(), ...)
?
到目前为止,我只发现第一种替代方法可能会导致问题,例如package:duckdb
。
英文:
I found no explicit guidance in DBI documentation on how to load the front-end (i.e. package:DBI
) and the back-end (e.g. package:odbc
).
Most examples in the documentation (but not all) attach package:DBI
to the search path and do not attach the back-end, e.g.:
library(DBI)
con <- dbConnect(odbc::odbc(), ...)
There was also a commit to favor this.
Why does the documentation use that method rather than e.g.:
# attaching only the back-end
library(odbc)
con <- dbConnect(odbc(), ...)
# attaching the back-end before attaching package:DBI
library(odbc)
library(DBI)
con <- dbConnect(odbc(), ...)
# attaching the back-end after attaching package:DBI
library(DBI)
library(odbc)
con <- dbConnect(odbc(), ...)
?
So far, I only found that the first alternative may cause issues, e.g. for package:duckdb
.
答案1
得分: 0
根据@r2evans的评论,package:odbc
(以及其他后端包)提供了所需的内部功能,但可能没有实现package:DBI
提供的每个单一方法。
这里是@DrNishaArora的一个示例,在仅附加后端时失败:
library(odbc)
conn <- dbConnect(odbc(), "xxx")
dbReadTable(conn, "A_TABLE_IN_THE_DB")
# Error in dbReadTable(conn, "A_TABLE_IN_THE_DB") :
# could not find function "dbReadTable"
当仅附加package:DBI
时,它按预期工作:
library(DBI)
conn <- dbConnect(odbc::odbc(), "xxx")
dbReadTable(conn, "A_TABLE_IN_THE_DB")
英文:
According to comments from @r2evans, package:odbc
(and other back-end packages) provide the internals required but may not implement every single method that package:DBI
provides.
Here is an example from @DrNishaArora where attaching only the back-end fails:
library(odbc)
conn <- dbConnect(odbc(), "xxx")
dbReadTable(conn, "A_TABLE_IN_THE_DB")
# Error in dbReadTable(conn, "A_TABLE_IN_THE_DB") :
# could not find function "dbReadTable"
It works as expected when attaching only package:DBI
:
library(DBI)
conn <- dbConnect(odbc::odbc(), "xxx")
dbReadTable(conn, "A_TABLE_IN_THE_DB")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论