Noobnoob
Noobnoob

Reputation: 27

RMySQL update row, not full table

Does anyone know how I can use RMySQL (or another library) to update a row in a table, rather than having to pull out the full table and push it back in? I don't want to read such a huge table into memory just to update one row.

What I am trying to do is pull out a row, change some of the values in there within R and push the same row object back into the table.

However, dbWriteTable seems to replace the entire table rather than just the row I specify.

Upvotes: 0

Views: 1620

Answers (2)

Kartheek Palepu
Kartheek Palepu

Reputation: 972

Using sqldf package:

library(sqldf)
table_name = data.frame(a = 1:10, b = 4)
# Open connection
sqldf()
fn$sqldf("update table_name set b=1")
ans = sqldf("select * from main.table_name")
# Close connection
sqldf()
print(table_name)

Upvotes: 0

Constantine
Constantine

Reputation: 488

The easiest way is to construct a string within R containing the adequate SQL Update statement and use dbSendQuery to push your data back into the table.

Upvotes: 3

Related Questions