0

How to pass the data frame d values in to postgresql statement inside like below :

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 mesg:

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"...

getting error for the above query.Suggest me if i was wrong.

4

1 回答 1

0

这是您传递给的内容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 ')"

您需要“折叠”d变量以获得类似('value1','value2',...):

sprintf("select date, amount from table where id in ('%s')", paste(d, collapse = "', '"))
[1] "select date, amount from table where id in ('u', 'i', 's', 't', 'k', 'l', 'y', 'v', 'n', 'z', 'b')"
于 2019-01-18T13:25:08.900 回答