odbcCloseAll的限制

时间:2011-12-15 19:33:01

标签: r odbc

我的R脚本必须做很多Excel打开/写入/关闭/打开/读取/关闭,中间有东西,我使用RODBC包。 odbcCloseAll似乎存在一个问题,它不会在多次关闭打开的Excel连接后关闭。 简单的例子(希望可重复,而不仅仅是因为我的电脑很奇怪):

require(RODBC)
filename <- tempfile(fileext='.xls')
for(i in 1:100) {
xlsFile <- odbcConnectExcel(filename, readOnly=FALSE)
sqlSave(xlsFile, USArrests, rownames = FALSE)
odbcCloseAll()
xlsFile <- odbcConnectExcel(filename, readOnly=FALSE)
template <- sqlFetch(xlsFile, "USArrests")
odbcCloseAll()
file.remove(filename)
}

在某些时候(在我的情况下大约i = 50),循环崩溃:

Error in sqlSave(xlsFile, USArrests, rownames = FALSE) : 
  table ‘USArrests’ already exists
In addition: Warning message:
In file.remove(filename) :
  cannot remove file 'c:\DOCUME~1\user\LOCALS~1\Temp\RtmpSFDDiG\file43522f58.xls', reason 'Permission denied'

使用odbcClose(xlsFile)或任何其他链接到Excel的包可以轻松解决问题,但为了正确起见,我想知道odbcCloseAll有什么问题...

1 个答案:

答案 0 :(得分:2)

在C源代码中发现它,C代码中存在错误:

 SEXP RODBCCloseAll(void)
 {
     int i;

     for(i = 1; i <= my_min(nChannels, 100); i++) <<<===== error
        if(opened_handles[i])
                  inRODBCClose(opened_handles[i]);

     return R_NilValue;
 }

应该是

 SEXP RODBCCloseAll(void)
 {
     int i;

     for(i = 1; i <= my_min(nChannels, 1000); i++) <<<=====
        if(opened_handles[i])
              inRODBCClose(opened_handles[i]);

     return R_NilValue;
 }

我会给开发者发电子邮件。与此同时,您可以使用此更改重建包。 错误在第1235行。

相关问题