Loading and/or attaching `package:DBI` and `package:odbc` et al.?

huangapple go评论77阅读模式
英文:

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 &lt;- dbConnect(odbc(), &quot;xxx&quot;)
dbReadTable(conn, &quot;A_TABLE_IN_THE_DB&quot;)
# Error in dbReadTable(conn, &quot;A_TABLE_IN_THE_DB&quot;) : 
#   could not find function &quot;dbReadTable&quot;

It works as expected when attaching only package:DBI:

library(DBI)
conn &lt;- dbConnect(odbc::odbc(), &quot;xxx&quot;)
dbReadTable(conn, &quot;A_TABLE_IN_THE_DB&quot;)

huangapple
  • 本文由 发表于 2023年3月7日 09:51:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定