mysql - How combine dbSendQuery with values from DataFrame in R? -
i'm looking way include data r
dataframe in sql predicate. ideally, i'd use dbsendquery
rmysql
package send query database contains where ... in
conditions includes values database. possible?
example data frame
bur lax lgb
example query
select * table airport in ('bur', 'lax', 'lgb')
is there way "pass" rows of data frame query? might not possible, i'm interested know.
i typically create "format" string, sub in values using sprintf
, paste
below:
qformat <- "select * table airport in (%s)" vals <- c("bur", "lax", "lgb") qstring <- sprintf(qformat, paste0("\"", vals, "\"", collapse = ",")) cat(qstring) # select * table airport in ("bur","lax","lgb")
if have lot, wrap messy part in function:
somefunc <- function(x) paste0("\"", x, "\"", collapse = ",") qstring <- sprintf(qformat, somefunc(vals))
if you're worried sql injection take @ ?dbescapestrings
.
Comments
Post a Comment