RODBCext查询参数

时间:2015-04-28 14:20:58

标签: sql r rodbc

我在这种类型的R中有sql查询:

    query <- "SELECT a.* FROM 
               ( (SELECT x,y,z FROM t1,t2,t3 where c1 = ? and c2 = ?) as b
                 LEFT JOIN
                 (SELECT x1,y1,z1 FROM t4, t5, t6 where c3 = ? and c4 = ?) as c
                  ON b.x = c.x1 ) as a"

我使用RODBCext连接和查询数据库。 我是这样做的:

    library(RODBCext)
    conHandle <- odbcConnect("dsn", uid="uid", pwd = "pwd", believeNRows = FALSE)
    parameters <- data.frame(v1,v2,v3,v4) #values for parameters
    response <- sqlExecute(conHandle, query, parameters, fetch = TRUE)

我收到此错误:

    Error in sqlExecute(conn, upit, parameters, fetch = TRUE) :
    [RODBCext] Error: Number of parameters in query do not match number of columns in data

对此有何帮助?

1 个答案:

答案 0 :(得分:0)

为任何人回答这个问题都在关注这个事实:

根据rodbcext包的github:由于参数数据帧中的参数数量(sql查询中的问号数量)与列数不匹配,因此抛出此错误。所犯的错误是在data.frame中包含一个向量会产生n行,而不是n列。

if(nparams != LENGTH(data))
{
  error(_("Number of parameters does not match number of columns in provided data"));
  FreeHandleResources(thisHandle);
  return 100;
}