Skip to content
Prev 1226 / 1559 Next

R and PostgreSQL - Writing data?

James,

I have never tried RPostgreSQL before but in ROracle which is also a DBI based interface you can do something like this:

library(ROracle)
con <- dbConnect(Oracle(), "scott", "tiger")
dbGetQuery(con, "create table foo as select * from emp")

dat <- dbGetQuery(con, "select * from foo")
dat$SAL <- dat$SAL*10
dbGetQuery(con, "update foo set sal = :1 where empno = :2",
           data = dat[,c("SAL","EMPNO")])
dbCommit(con)
dbGetQuery(con, "select * from foo")

dbGetQuery(con, "drop table foo purge")
dbDisconnect(con)

Denis

-----Original Message-----
From: Sean Davis [mailto:sdavis2 at mail.nih.gov] 
Sent: Friday, September 28, 2012 11:43 AM
To: James David Smith
Cc: r-sig-db at r-project.org
Subject: Re: [R-sig-DB] R and PostgreSQL - Writing data?
On Fri, Sep 28, 2012 at 10:36 AM, James David Smith <james.david.smith at gmail.com> wrote:
To do an update, you'll need to loop through your data.frame and issue a dbSendQuery().  To create the SQL string, I often use something
like:

sprintf("UPDATE originalTable SET ucam_no2=%f WHERE originalTable.id = %d",....)

You can't do this in one step, unfortunately.  This is how UPDATE works and has nothing to do with R.

Sean
_______________________________________________
R-sig-DB mailing list -- R Special Interest Group R-sig-DB at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-db