经过大量测试后,我发现R包装RODBC中有一个混乱的特征。使用函数RODBC::odbcDriverConnect
,我无法连接到R工作目录中的mdb文件。一位同事建议我将./
添加到相对路径的文件名中。这样做,连接工作。要测试这个,您需要创建自己的MS Access mdb文件。我想这是MS驱动程序的特征,而不是RODBC?
filename <- "test.mdb"
filename.relative <- paste0("./", filename)
driver.name <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
driver.name <- paste0(driver.name, "DBQ=", filename)
#fails:
con <- RODBC::odbcDriverConnect(driver.name)
我得到的错误信息是:
Warning messages:
1: In RODBC::odbcDriverConnect(driver.name) :
[RODBC] ERROR: state HY000, code -1044, message [Microsoft]
[ODBC Microsoft Access Driver] Not a valid file name.
2: In RODBC::odbcDriverConnect(driver.name) : ODBC connection failed
#Second try:
driver.name <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
driver.name <- paste0(driver.name, "DBQ=", filename.relative)
#works given it includes relative path:
con <- RODBC::odbcDriverConnect(driver.name)
RODBC::odbcCloseAll()