8

How do I update data in a postgresql db through R with new data?

I've tried

dbGetQuery(con,"UPDATE table SET column1=:1,column2=:2, column3=:3 
              where id=:4", data=Rdata[,c("column1", "column3", "column3","id")])

I also tried with the colons replaced with $ but that didn't work either. I keep getting:

Error in postgresqlExecStatement(conn, statement, ...) : 
unused argument(s)
1
  • Have you looked at the documentation to see if placeholders are supported at all? Not every db interface from R does. I know RSQLite does, but I'm pretty sure RODBC does not, and I'm not sure about RPostgreSQL. Commented Feb 26, 2013 at 21:35

2 Answers 2

13

I figured it out using:

update <- function(i) {
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname="db_name", host="localhost", port="5432", user="chris", password="password")
txt <- paste("UPDATE data SET column_one=",data$column_one[i],",column_two=",data$column_two[i]," where id=",data$id[i])
dbGetQuery(con, txt)
dbDisconnect(con)
}


registerDoMC()

foreach(i = 1:length(data$column_one), .inorder=FALSE,.packages="RPostgreSQL")%dopar%{
update(i)
}
Sign up to request clarification or add additional context in comments.

Comments

1

At least the RODBC has a specific function sqlUpdate:

sqlUpdate updates the table where the rows already exist. Data frame dat should contain columns with names that map to (some of) the columns in the table

See http://cran.r-project.org/web/packages/RODBC/RODBC.pdf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.