Just wondering if it is possible to pass parameters to the SQL query IN clause using DBI? Have tried the following (and many variations, including unnamed parameters)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "iris", iris)
iris_result <- dbSendQuery(con, "SELECT * FROM iris WHERE [Petal.Width] > $PW and [Petal.Length] in ($PL)")
dbBind(iris_result, list(PW=2.3, PL={6.0, 5.1}))
dbFetch(iris_result)
This link, Parameterized Queries, shows a method using the glue package, however, I would like to know if it is possible with just DBI.
Thanks.
Note, for reference, here is the method using glue:
rs_sql <- glue_sql("SELECT * FROM iris WHERE [Petal.Width] > {pwin} and [Petal.Length] IN ({lengths*})",
pwin = 2.3, lengths = c(6.0, 5.1),
.con = con
)
iris_result <- dbSendQuery(con, rs_sql)
dbFetch(iris_result)
rs_sql <- glue_sql("SELECT * FROM iris WHERE [Petal.Width] > {pwin} and [Species] IN ({species*})",
pwin = 2.3,
species = c('virginica'),
.con = con
)
iris_result <- dbSendQuery(con, rs_sql)
dbFetch(iris_result)