如何将数据帧值解析为postgresql语句?

时间:2019-01-18 12:28:08

标签: r postgresql

如何将数据帧d的值传递到内部的postgresql语句中,如下所示:

a<-data.frame(b=sample(letters),c=1:26)

d<-a%>%filter(c>15)%>%select(b)

e<-paste("select date,amount from table where id in ('", d,"')")
dbGetQuery(con,e)

错误消息:

Error in postgresqlExecStatement(conn, statement, ...) : 
RS-DBI driver: (could not Retrieve the result : ERROR:  invalid input syntax for integer: "c("m","w"...)
LINE 1: ...type,id from table where id in ('c("m","w"...

获取上述查询错误。如果我错了,建议我。

1 个答案:

答案 0 :(得分:0)

这是您传递给Cannot iterate over string ("2")的内容:

dbGetQuery

您需要“折叠” paste("select date,amount from table where id in ('", d,"')") [1] "select date,amount from table where id in (' u ')" "select date,amount from table where id in (' i ')" [3] "select date,amount from table where id in (' s ')" "select date,amount from table where id in (' t ')" [5] "select date,amount from table where id in (' k ')" "select date,amount from table where id in (' l ')" [7] "select date,amount from table where id in (' y ')" "select date,amount from table where id in (' v ')" [9] "select date,amount from table where id in (' n ')" "select date,amount from table where id in (' z ')" [11] "select date,amount from table where id in (' b ')" 变量以获取类似(“ value1”,“ value2”,...)的信息:

d