dbHasCompleted always returns TRUE

生来就可爱ヽ(ⅴ<●) 提交于 2020-06-13 06:19:29

问题


I'm using R to do a statistical analysis on a SQL Server 2008 R2 database. My database client (aka driver) is JDBC and thereby I'm using RJDBC package.

My query is pretty simple and I'm sure that query would return a lot of rows (about 2 million rows).

SELECT * FROM [maindb].[dbo].[users]

My R script is as follows.

library(RJDBC);

javaPackageName <- "com.microsoft.sqlserver.jdbc.SQLServerDriver";
clientJarFile <- "/home/abforce/mystuff/sqljdbc_3.0/enu/sqljdbc4.jar";
driver <- JDBC(javaPackageName, clientJarFile);
conn <- dbConnect(driver, "jdbc:sqlserver://192.168.56.101", "username", "password");

query <- "SELECT * FROM [maindb].[dbo].[users]";
result <- dbSendQuery(conn, query);
dbHasCompleted(result)

In the codes above, the last line always returns TRUE. What could be wrong here?


回答1:


The fact of function dbHasCompleted always returning TRUE seems to be a known issue as I've found other places in the Internet where people were struggling with this issue.

So, I came with a workaround. Instead of function dbHasCompleted, we can use conditional statement nrow(result) == 0.

For example:

result <- dbSendQuery(conn, query);
repeat {
    chunk <- dbFetch(result, n = 10);
    if(nrow(chunk) == 0){
        break;
    } 
    # Do something with 'chunk';
}
dbClearResult(result);


来源:https://stackoverflow.com/questions/33735086/dbhascompleted-always-returns-true

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