Reputation: 73
I am looking to analyse data in R (using dplyr) contained in an Access database on my laptop. (My first time trying to set up a database connection in R.)
Looking at the tidyverse site, for dplyr to work on the Access data, it seems that the connection must be via the DBI package (rather than RODBC).
I'm struggling with the syntax of dbConnect.
My code for RODBC was
base1<-odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=[filepath]/AdventureWorks DW 2012.accdb")
My (failed) attempt for DBI is
DB <- dbConnect(drv=Microsoft Access Driver (*.mdb, *.accdb)), host=[filepath]/AdventureWorks DW 2012.accdb)
What am I doing wrong?
(I'm working on Windows 10 - everything 64 bit.)
Upvotes: 7
Views: 5520
Reputation: 11
As explained here, you can still use odbcConnectAccess2007()
but need 32-bit MS Access 2007 drivers from here. This worked great for me.
Upvotes: 0
Reputation: 749
I recently needed to convert my RODBC defined db connections to equivalent DBI connections. Here's the original RODBC function:
connect_to_access_rodbc <- function(db_file_path) {
require(RODBC)
# make sure that the file exists before attempting to connect
if (!file.exists(db_file_path)) {
stop("DB file does not exist at ", db_file_path)
}
# Assemble connection strings
dbq_string <- paste0("DBQ=", db_file_path)
driver_string <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
db_connect_string <- paste0(driver_string, dbq_string)
myconn <- odbcDriverConnect(db_connect_string)
return(myconn)
}
As explained here, the dbplyr package is built from the DBI package. The first argument of the DBI::dbConnect()
must be an appropriate back-end driver. See the link for a list of drivers. For Access, the odbc::odbc()
driver is suitable. The second argument the dbConnect function is the full connection string as used in the previous odbcDriverConnect call. With that in mind the following function should connect to your access database:
connect_to_access_dbi <- function(db_file_path) {
require(DBI)
# make sure that the file exists before attempting to connect
if (!file.exists(db_file_path)) {
stop("DB file does not exist at ", db_file_path)
}
# Assemble connection strings
dbq_string <- paste0("DBQ=", db_file_path)
driver_string <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
db_connect_string <- paste0(driver_string, dbq_string)
myconn <- dbConnect(odbc::odbc(),
.connection_string = db_connect_string)
return(myconn)
}
The odbc package documentation presents a more nuanced example as well: https://github.com/r-dbi/odbc#odbc
Upvotes: 11
Reputation: 20302
I just used this a couple days ago.
library(RODBC)
# for 32 bit windows
# Connect to Access db
# channel <- odbcConnectAccess("C:/Users/Excel/Desktop/Coding/Microsoft Access/Northwind.mdb")
# Get data
# data <- sqlQuery( channel , paste ("select * from Name_of_table_in_my_database"))
# for 64 bit windows
channel <- odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/Users/Excel/Desktop/Coding/Microsoft Access/Northwind.mdb")
data <- sqlQuery( channel , paste ("select * from CUSTOMERS"))
odbcCloseAll()
Upvotes: -2