RPostgres (version 1.3.1)

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, ...)

# S4 method for PqConnection dbCommit(conn, ...)

# S4 method for PqConnection dbRollback(conn, ...)

Arguments

conn

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

...

Unused, for extensibility.

Value

A boolean, indicating success or failure.

Examples

Run this code
# NOT RUN {
# For running the examples on systems without PostgreSQL connection:
run <- postgresHasDefault()

library(DBI)
if (run) con <- dbConnect(RPostgres::Postgres())
if (run) dbWriteTable(con, "USarrests", datasets::USArrests, temporary = TRUE)
if (run) dbGetQuery(con, 'SELECT count(*) from "USarrests"')

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

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

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

Run the code above in your browser using DataCamp Workspace