将 R 变量传递给 sql 语句 [英] Pass R variable to a sql statement

查看:33
本文介绍了将 R 变量传递给 sql 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以将R中定义的变量传递给sqldf包中的SQL语句?

Is there any way to pass a defined variable in R to the SQL statement within the sqldf package?

我必须运行下面的代码,我将 'v' 变量作为 '$v' 传递给 sql select 语句

i have to run the code below and I passed the 'v' variable to sql select statement as '$v'

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

虽然运行了,但是返回错误的结果.[结果应该是 1000 但此代码返回 0].

Although it runs, it returns wrong result. [The result should be 1000 but this code returns 0].

因此,我认为它不会传递变量值.

Hence, I think it doesn't pass the variable value.

推荐答案

如果 v 是一个整数,那么您不想用单引号将 $v 括起来 - 这使它成为一个字符串值.尝试不使用单引号.

If v is an integer then you don't want to enclose the $v with single quotes - that makes it a string value. Try without the single quotes.

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

这篇关于将 R 变量传递给 sql 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆