[R] RPostgreSQL (or even ANSI DBI) parameterized query with IN (...)

Murat Tasan mmuurr at gmail.com
Fri Dec 4 17:52:57 CET 2015


Using PostgreSQL's parameterized query form, this works:

R> dbSendQuery(CONN, "SELECT * FROM foo WHERE val = $1 OR val = $2",
list("bar", "baz"))

... and becomes: SELECT * FROM foo WHERE val = 'bar' OR val = 'baz';

I cannot figure out, however, if something like this is possible with
RPostgreSQL:

R> dbSendQuery(CONN, "SELECT * FROM foo WHERE val IN ($1)",
list(c("bar", "baz")))

... which becomes: SELECT * FROM foo WHERE val IN ('bar', 'baz');

(To be clear, the dbSendQuery attempt above does _not_ work.)

Anyone know if this is doable with RPostgreSQL?
I can construct the statement with sprintf/paste, but I'd prefer to
use properly-parameterized queries if possible (relying on PostgreSQL
to do the type conversions safely).

I've also, BTW, tried using DBI's basic ANSI functionality, like so:

R> sqlInterpolate(ANSI(), "SELECT * FROM foo WHERE name IN (?names)",
names = c("foo", "bar"))

... but this also doesn't work :-/

-Murat



More information about the R-help mailing list