1

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

1 Answer 1

2

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 ")
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it is an integer. I tried it before but i got an error of not recognizing the $ symbol! and now, I ran it one more time and i got this error: Error in sqliteExecStatement(con, statement, bind.data) : RS-DBI driver: (incomplete data binding: expected 1 parameters, got 0)
you need to invoke it with fn$sqldf to get the interpolation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.