简体   繁体   中英

How to connect to .accdb database from R

New to R and have having troubles connecting to Access databases. This is the code I am running (RODBC package loaded manually before running):

library(RDOBC)

conn <- odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='C:/db/dbNorthwind.accdb'")
subset(sqlTables(conn), TABLE_TYPE == "TABLE") 
df <- sqlFetch(conn, "tblCustomer") 
df # 

I get the error-message:

Error in sqlTables(conn) : first argument is not an open RODBC channel.

Know any tips on how to fix?

environment:

  • 64-bit Windows
  • 32-bit MS Access 2013
  • 64-bit Microsoft R Open

The code you're using is fine, your setup isn't.

You need to either use 64-bits R and 64-bits MS Access, or 32-bit R and 32-bit MS Access. You can try, however, to install 64-bits Access Database Engine, found here (2016 version) or here (2010 version) .

I have had varying results with installing both the full version of Access and the database engine (currently got Access 32-bits 2016, and couldn't install Access database engine 64-bits 2016, but could install the 2010 version by using the /passive command on the installer).

See this blog for some details on how to install the 32-bits full version and the 64-bits database engine on one machine. But as noted, results may vary.

I do not know much about database import, but I will try to help you by telling you what I usually do. The first thing that you have to do is to understand if you have the DSN in order to access the database. You can do it by clicking start and going to ODBC Data Source. When you are there in the window "UserDSN" you check if there is your file. In case it is not there you just need to add it, by clicking in "add" and then you look for the driver to read your file in your case is .accdb. In case you do not have the driver you can download it here:

http://www.microsoft.com/downloads/details.aspx?familyid=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=e

After you have created you DSN then you go into R and you write this few lines of code. Here I will write an example. I have downloaded an access file from:

https://www.599cd.com/access/studentdatabases/

Access file : Access Beginner 1

The in RI have typed:

library(RODBC)


odbcDS <- "PCResale Customer Database"

tmp <- odbcConnect(dsn = odbcDS)

df <- sqlFetch(tmp, "CustomerT") 

Hope that this is of any help,

Ciao!

This worked fine for me.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM