R矢量成字符串的字符串

hjw*_*hjw 4 sql r paste

我是R和mySQL的新手,想在R中运行以下mysql命令

query = "select x, y from table where z in ('a', 'b');"
sqlQuery(connection, query)
Run Code Online (Sandbox Code Playgroud)

假设我有一个很长的可变长度向量.有可能吗?

vector = c('a','b', .....)
query = "select x, y from table where z in **vector**;"
Run Code Online (Sandbox Code Playgroud)

我试过了

query = paste("select x, y from table where z in (", paste(vector, collapse =', '), ");")
Run Code Online (Sandbox Code Playgroud)

但我在括号中丢失了引号而且我得到了

query = "select x, y from table where z in (a, b);"
Run Code Online (Sandbox Code Playgroud)

它不在sqlQuery中运行.有没有办法使用粘贴命令,以便我得到一串字符串?或者有更好的方法来完成我想要完成的任务吗?

dic*_*koa 10

你需要使用 shQuote

query <- paste("select x, y from table where z in (", paste(shQuote(vector, type = "sh"), 
         collapse = ', '), ");")
query
[1] "select x, y from table where z in ( 'a', 'b', 'c', 'd' );"
Run Code Online (Sandbox Code Playgroud)