简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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