ODBC connection string and/or DSN in R

有些话、适合烂在心里 提交于 2020-04-16 02:57:05

问题


I'm trying to connect to and read many hundreds of MS Access database files using the "RODBC" package in RStudio (32 bit). The previous program used to open and read these files used and system.mdw system database file to specify user permissions, but I can't find anything in the RODBC documentation that describes how to include this file in the connection string. Here is what I've tried, which has succceded in connecting to the database, but failed to read any of the contents:

files <- dir("file directory to access files", recursive=TRUE, full.names=TRUE, pattern="\\.mdb$")              

#2007 version is able to connect but can't read
dta <- odbcConnectAccess2007(files[1])
tables = sqlTables(dta)# can read all the table names in each file

  habData = sqlFetch(dta, "Streams") #can't read the table content, see error message below

> habData
[1] "42000 -1907 [Microsoft][ODBC Microsoft Access Driver] Record(s) cannot be read; no read permission on 'Streams'."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'SELECT * FROM \"Streams\"'" 

#Older version is able to connect and still can't read
conn <- odbcConnect(paste("MS Access Database;DBQ=",files[4],sep = ""), uid = "Engine")
df <- sqlFetch(conn, "Streams") #can't read the table content, see error message below

I have the system.mdw file, but don't know how to relate it to the Access database. Has anyone done this in R? Perhaps with a different package?


回答1:


I dissected the connection string established with the windows ODBC control panel DSN and used that to reconstruct a new connection string incorporating the system.mdw file, userID and password. Now I can run a loop through all the file directories and read out the data!




回答2:


I haven't done it in R, but the R documentation for ODBCconnect says that the first parameter is "DSN" (Data Source Name). Rather than using a parameterized connect string as in your "Older version", I suggest that you start at the "ODBC" icon in the Windows Control Panel. That will give you a wizard that helps you fill in all the needed details. The wizard will save the information in the registry or in a file, and all you need to give ODBCconnect in R is the name or full location of the DSN it has created.

I don't suggest using ODBCconnect2007 - it looks like a simplified interface that doesn't have all the options you need.



来源:https://stackoverflow.com/questions/60644707/odbc-connection-string-and-or-dsn-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!