rodbcdbi

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


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.


Solution

  • According to comments from @r2evans and @hadley, package:odbc (and other back-end packages) provide the needed methods but does not re-export the DBI generics. The expectation is that we load package:DBI to get them.

    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")