简体   繁体   English

将R变量传递给sql语句

[英]Pass R variable to a sql statement

Is there any way to pass a defined variable in R to the SQL statement within the sqldf package? 有什么方法可以将R中的已定义变量传递给sqldf包中的SQL语句?

i have to run the code below and I passed the 'v' variable to sql select statement as '$v' 我必须运行下面的代码,并将“ v”变量传递给sql select语句为“ $ v”

 for (i in 1:50){
          v <- i+ 450
          temp <- sqldf("select count(V1) from file_new where V1='$v' ")
        }

Although it runs, it returns wrong result. 尽管运行,但返回错误结果。 [The result should be 1000 but this code returns 0]. [结果应为1000,但此代码返回0]。

Hence, I think it doesn't pass the variable value. 因此,我认为它不会传递变量值。

If v is an integer then you don't want to enclose the $v with single quotes - that makes it a string value. 如果v是一个整数,则您不想用单引号将$ v括起来-这使它成为字符串值。 Try without the single quotes. 尝试不使用单引号。

temp <- fn$sqldf("select count(V1) from file_new where V1=$v ")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM