英文:
Pass multiple parameters in R SQL query
问题
我正在尝试在R SQL查询中多次传递相同的参数。查询如下:
table_x <- c(‘Rain’, ‘Cloudy’)
df <- dbGetQuery(conn, "select schemaname, tablename, dateupdated, test_result from weather
Where dateupdated in (select max(dateupdated) from weather where conditions in (?,?))
and dateupdated = sysdate
and conditions in (?,?)",
params= table_x)
如果我删除第二个conditions子句,查询就可以工作。否则会报错:
> 查询需要4个参数;提供了2个
我尝试创建另一个向量,并将其与第一个向量一起传递,但仍然收到相同的错误。有什么建议吗?
谢谢
英文:
I am trying to pass same parameters multiple times in R SQL query. The query is:
table_x <- c(‘Rain’, ‘Cloudy’)
df <- dbGetQuery(conn, "select schemaname, tablename, dateupdated, test_result from weather
Where dateupdated in (select max(dateupdated) from weather where conditions in (?,?))
and dateupdated = sysdate
and conditions in (?,?)",
params= table_x)
The query works if I remove the 2nd conditions clause. Otherwise it complains
> Query requires 4 parameters; 2 supplied
I tried creating another vector and pass that through along with 1st vector but still getting same error. Any suggestions.
Thanks
答案1
得分: 1
对于查询中找到的每个 ?
,您必须在 params=
中有一个参数。顺序很重要,所以 params=c(table_x, table_x)
应该有效。
df <- dbGetQuery(conn, "
select schemaname, tablename, dateupdated, test_result
from weather
where dateupdated in (select max(dateupdated) from weather where conditions in (?,?))
and dateupdated = sysdate
and conditions in (?,?)",
params = c(table_x, table_x))
英文:
For every ?
found in the query, you must have a parameter in params=
. Order is important, so params=c(table_x, table_x)
should work.
df <- dbGetQuery(conn, "
select schemaname, tablename, dateupdated, test_result
from weather
where dateupdated in (select max(dateupdated) from weather where conditions in (?,?))
and dateupdated = sysdate
and conditions in (?,?)",
params = c(table_x, table_x))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论