简体   繁体   English

在 R 脚本中传递字符串变量以在 SQL 语句中使用它

[英]Pass string variable in R script to use it in SQL statement

I tried use a string variable in R script to use through SQL statement for example:我尝试在 R 脚本中使用字符串变量通过 SQL 语句使用,例如:

x="PASS"

SQL<- paste("select ID, NAME, STATUS from STUDENT where STATUS =(",x,")",sep="")
Q1 <- dbGetQuery(con, SQL)

The ERROR says:错误说:

Error in mysqlExecStatement(conn, statement, ...) : mysqlExecStatement(conn, statement, ...) 中的错误:
RS-DBI driver: (could not run statement: Unknown column 'PASS' in 'where clause') RS-DBI 驱动程序:(无法运行语句:“where 子句”中的未知列“PASS”)

That means STATUS =(",x,")" = PASS and it must 'PASS' with add quote ''这意味着 STATUS =(",x,")" = PASS 并且它必须'PASS'并添加引号''

I tried to put the '' but no success as the following.我试图把''但没有成功如下。

SQL <- paste("select ID, NAME, STATUS from STUDENT where STATUS ='(",x,")' ",sep="")
Q1 <- dbGetQuery(con, SQL)

I tested it with number and it is work well but when I use string it is not working, because the value must be in quotes ' ' .我用 number 测试了它,它运行良好,但是当我使用 string 时它不起作用,因为该值必须在引号中' '

Use sprintf instead:使用sprintf代替:

x <- "PASS"
sprintf("select ID, NAME, STATUS from STUDENT where STATUS = '%s'", x)

## [1] "select ID, NAME, STATUS from STUDENT where STATUS = 'PASS'"

Try this:试试这个:

library(gsubfn)
x <- "PASS"

fn$dbGetQuery(con, "select ID, NAME, STATUS from STUDENT where STATUS = '$x' ")

This also works:这也有效:

s <- fn$identity("select ID, NAME, STATUS from STUDENT where STATUS = '$x' ")
dbGetQuery(con, s)

EDIT for windows编辑窗口

Try尝试

x = "PASS"

SQL<- paste0("select ID, NAME, STATUS from STUDENT where STATUS = ", shQuote(x, 'sh'))
Q1 <- dbGetQuery(con, SQL)

More generally shQuote is useful for constructed things like:更一般地说, shQuote对构造的东西很有用,例如:

paste0("SELECT * FROM urtable where urvar IN(", paste0(shQuote(LETTERS, 'sh'), collapse = ','), ")")
[1] "SELECT * FROM urtable where urvar IN('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')"

 #

If you have simple character strings.如果您有简单的字符串。 For more complicated character strings other approaches maybe necessary.对于更复杂的字符串,可能需要其他方法。 For example in PoSTgreSQL you can use Dollar-Quoted String Constants to escape characters.例如在 PoSTgreSQL 中,您可以使用带Dollar-Quoted String Constants来转义字符。

You dont mention what variant of SQL you are using or associated R package.您没有提及您正在使用的 SQL 变体或关联的 R 包。 Some R packages may have helper functions like postgresqlEscapeStrings in RPostgreSQL or dbEscapeStrings in RMySQL .一些R程序包可能有类似的辅助函数postgresqlEscapeStringsRPostgreSQLdbEscapeStringsRMySQL

Use glue_sql() from glue package.使用glue 包中的glue_sql()。

x = "PASS" x = "通过"

glue_sql("select ID, NAME, STATUS from STUDENT where STATUS = {x}", .con = con) glue_sql("select ID, NAME, STATUS from STUDENT where STATUS = {x}", .con = con)

see more examples here: https://glue.tidyverse.org/#glue_sql-makes-constructing-sql-statements-safe-and-easy在此处查看更多示例: https : //glue.tidyverse.org/#glue_sql-makes-constructing-sql-statements-safe-and-easy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM