Skip to content

Instantly share code, notes, and snippets.

@tomsing1
Created April 19, 2024 17:33
Show Gist options
  • Save tomsing1/86a026f4230bc28fd9469c810dcdfc38 to your computer and use it in GitHub Desktop.
Save tomsing1/86a026f4230bc28fd9469c810dcdfc38 to your computer and use it in GitHub Desktop.
Controlling quoting by the glue::glue_sql() function
library(glue)
library(RSQLite)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
var = "test"
glue_sql("{var}", .con = con) # <SQL> 'test'
glue_sql("{`var`}", .con = con) # <SQL> `test`
glue_sql("{DBI::SQL(var)}", .con = con) # <SQL> test (unquoted)
glue_sql("`{var}`", .con = con) # <SQL> `'test'` NOT USEFUL
var = c("test1", "test2")
glue_sql("{var*}", .con = con) # <SQL> 'test1', 'test2'
glue_sql("{`var`}", .con = con) # <SQL> `test1` (newline) <SQL> `test2`
glue_sql("{DBI::SQL(var)}", .con = con) # <SQL> test1 (newline) <SQL> test2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment