RPostgres (version 1.4.4)

postgres-transactions: Transaction management.

Description

dbBegin() starts a transaction. dbCommit() and dbRollback() end the transaction by either committing or rolling back the changes.

Usage

# S4 method for PqConnection
dbBegin(conn, ..., name = NULL)

# S4 method for PqConnection dbCommit(conn, ..., name = NULL)

# S4 method for PqConnection dbRollback(conn, ..., name = NULL)

Arguments

conn

a '>PqConnection object, produced by DBI::dbConnect()

...

Unused, for extensibility.

name

If provided, uses the SAVEPOINT SQL syntax to establish, remove (commit) or undo a <U+00DF>savepoint.

Value

A boolean, indicating success or failure.

Examples

Run this code
# NOT RUN {
library(DBI)
con <- dbConnect(RPostgres::Postgres())
dbWriteTable(con, "USarrests", datasets::USArrests, temporary = TRUE)
dbGetQuery(con, 'SELECT count(*) from "USarrests"')

dbBegin(con)
dbExecute(con, 'DELETE from "USarrests" WHERE "Murder" > 1')
dbGetQuery(con, 'SELECT count(*) from "USarrests"')
dbRollback(con)

# Rolling back changes leads to original count
dbGetQuery(con, 'SELECT count(*) from "USarrests"')

dbRemoveTable(con, "USarrests")
dbDisconnect(con)
# }

Run the code above in your browser using DataLab