R中的ODBC连接字符串和/或DSN [英] ODBC connection string and/or DSN in R

查看:151
本文介绍了R中的ODBC连接字符串和/或DSN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用RStudio(32位)中的"RODBC"包连接并读取数百个MS Access数据库文件.先前的程序用来打开和读取这些文件,并使用system.mdw系统数据库文件来指定用户权限,但是我在RODBC文档中找不到任何描述如何在连接字符串中包含此文件的内容.这是我尝试过的,已成功连接到数据库,但无法读取任何内容:

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

我有system.mdw文件,但不知道如何将其与Access数据库关联.有人在R中完成此操作吗?也许使用其他包装?

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?

推荐答案

我剖析了使用Windows ODBC控制面板DSN建立的连接字符串,并使用它重建了包含system.mdw文件,用户ID和密码的新连接字符串.现在,我可以遍历所有文件目录并读取数据!

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!

这篇关于R中的ODBC连接字符串和/或DSN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆