如何比较sql中具有特殊字符的值?

ia2d9nvy  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(353)

在r中运行dbsendquery时,我不断收到错误。我在sql中比较的值与以前选择的值都有一个连字符和空格(两个值都有)。它一直给我一个错误,说“错误的语法附近的'米格'。我不知道如何避开这个问题。提前谢谢!
更新:上面说我不能发布图片,但我会在下面写代码


# con1 is my connection to SQL

# input$first is my label from a select Input

Df1 <- dbSendQuery(con1, glue_sql("SELECT [redacted column Name] FROM [redacted table name] WHERE [redacted column name] = '{input$first}'", .con = con1))

两者 input$first 第二个经过编辑的列名的值中有空格和连字符

uyto3xhc

uyto3xhc1#

在docs and rstudio教程中,不应引用占位符表达式:

query <- dbSendQuery(con1, glue_sql("SELECT [redacted column Name] 
                                     FROM [redacted table name] 
                                     WHERE [redacted column name] = {param}",
                                     param  = input$first,
                                    .con = con1)
DF1 <- dbFetch(query, con1)

如果 input$first 包含多个值,请调整sql以使用 IN :

WHERE [redacted column name] IN {param*}

或者,使用dbi的参数化方法, sqlInterpolate ,在大多数情况下都应该有效 DBI Package (例如。, RPostgreSQL , ROracle , RMySQL , odbc ):

sql <- sqlInterpolate(con, 
  "SELECT [redacted column Name] 
   FROM [redacted table name] 
   WHERE [redacted column name] =  ?param", 
  param = input$first
)

DF1 <- dbGetQuery(con, sql)

相关问题