问题描述
我正在使用 R 对 SQL Server 2008 R2 数据库进行统计分析.我的数据库客户端(又名驱动程序)是 JDBC,因此我使用的是 RJDBC 包.
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.
我的查询非常简单,我确信该查询会返回很多行(大约 200 万行).
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]
我的R脚本如下.
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)
在上面的代码中,最后一行总是返回 TRUE
.这里有什么问题?
In the codes above, the last line always returns TRUE
. What could be wrong here?
推荐答案
函数 dbHasCompleted
总是返回 TRUE
的事实似乎是我发现的一个已知问题互联网上人们正在努力解决此问题的其他地方.
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.
所以,我想出了一个解决方法.代替函数dbHasCompleted
,我们可以使用条件语句nrow(result) == 0
.
So, I came with a workaround. Instead of function dbHasCompleted
, we can use conditional statement nrow(result) == 0
.
例如:
result <- dbSendQuery(conn, query);
repeat {
chunk <- dbFetch(result, n = 10);
if(nrow(chunk) == 0){
break;
}
# Do something with 'chunk';
}
dbClearResult(result);
这篇关于dbHasCompleted 始终返回 TRUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!