Stéphane Laurent
Stéphane Laurent

Reputation: 84529

Issue with `DBI::dbGetQuery` run from a Shiny app

I have a Shiny app with which the user selects a table in a SQL database and then selects some columns of this table. Then the app runs this function to get the table:

selectAllGetQuery <- function(conn, columns, table){
  columns <- toString(sprintf("[%s]", columns))
  query <- sprintf("select %s from %s", columns, table)
  print(columns) # ok
  print(query) # ok
  dbGetQuery(conn, query)
}

One table has a column named ACT_TEMP_°C_. When I select it, the dbGetQuery statement fails: Invalid column name 'ACT_TEMP_°C_'. However, as you can see, I included print(columns) and print(query) in the function, and these statements correctly print ACT_TEMP_°C_ in the R console. So I'm really lost. The function receives the correct name but the name changes to the wrong name in dbGetQuery.

Upvotes: 2

Views: 207

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

Solved with

dbGetQuery(conn, stringi::stri_enc_tonative(query))

Upvotes: 1

Related Questions