2016-07-29 136 views
1

我需要知道为什么会出现提到错误,尽管当我用任何数值代替DFinal [i,“x”]时工作正常,感谢你的帮助sqliteSendQuery(con,statement,bind.data)中的错误:error in statement:[

for(i in 1:nrow(DFinal)){ 
+  result =0 
+  var1= DFinal[i,"x"] 
+  var2= DFinal[i,"y"] 
+  result <- sqldf(' select count(distinct(V5)) from DLoc where V1= DFinal[i,"x"] and V5 in (select distinct(V5) from DLoc where V1=var2) ') 
+  
+  DFinal[i,"res"]<- result 
+  
+ } 

错误sqliteSendQuery(CON,声明,bind.data): 错误的语句:近 “[我,” X “]”:语法错误

回答

0

您查询没有任何意义,因为您指的是R变量,而是使用变量'的值

for (i in 1:nrow(DFinal)) { 
    result <- 0 
    var1 <- DFinal[i, "x"] 
    var2 <- DFinal[i, "y"] 
    query <- paste0('select count(distinct V5) from DLoc where V1 = ', var1, 
        ' and V5 in (select distinct V5 from DLoc where V1 = ', var2, ')') 
    result <- sqldf(query) 

    DFinal[i, "res"] <- result 
} 

您的查询看起来可能还需要一些改进,但上面的代码应该至少允许您运行它。

另一种评论:通常不建议将原始SQL查询连接在一起,因为它会打开SQL注入攻击的可能性。但是,当你在一个可能是封闭的R环境中运行这个查询时,它可能是正常的。