Pass R variable to a sql statement

雨燕双飞 提交于 2019-12-19 11:36:39

问题


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

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' ")
        }

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.


回答1:


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 ")


来源:https://stackoverflow.com/questions/22861915/pass-r-variable-to-a-sql-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!