Skip to content
Prev 343056 / 398506 Next

pass vector binding to DBI parameter (rsqlite)

On Tue, Aug 12, 2014 at 10:55 AM, Dan Muresan <danmbox at gmail.com> wrote:
I don't really _know_ much, but what I would try would be something like:

dbGetQuery(c,"select * from tst where y not in (?)",paste(c(7,6),collapse=','));

The paste(c(7,6),collapse=',') results in the string "6,7". You could
always subject yourself to a SQL injection attack by doing:

dbGetQuery(c,paste("select * from tst where y not in
(",c(7,6),")",collapse=','));

If you do this and use a variable instead of the c(7,6), make sure you
"cleanse" the contents of the variable. Just as making sure that there
is no "bare" semi-colon in it. And other things that don't come to
mind off hand.

Hum, perhaps better:

values<-c(7,6);
dbGetQuery(c,paste("select * from tst where y not in (",
                                paste(rep('?',length(values)),collapse=','),
                                ")"),
                        values);

As you can see, this dynamically adjusts the number of ? marks in the
SELECT statement, based on the number of elements in the "values"
variable.