How to glue nothing into sql?

I'm build a shiny app.

I need to query database based on user input. I guess I need to glue nothing into sql when user keep the default input values unchanged and glue something into the sql when user modify the default input values.

library(glue)

con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
colnames(iris) <- gsub("[.]", "_", tolower(colnames(iris)))
DBI::dbWriteTable(con, "iris", iris)

empty <- NULL

value <- as.symbol("ORDER BY sepal_width")

glue_sql("SELECT * FROM iris {empty}", .con = con)
#> <SQL>

glue_sql("SELECT * FROM iris {value}", .con = con)
#> Warning in is.na(res): is.na() applied to non-(list or vector) of type
#> 'symbol'
#> <SQL> SELECT * FROM iris ORDER BY sepal_width

Created on 2019-04-12 by the reprex package (v0.2.1)

Just find glue DBI::SQL('') into the statement helps me. Not sure is it desinged to work like this or it is just a coincidence. So I create a issue in glue repo.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.