R/dbBegin_PqConnection.R
, R/dbCommit_PqConnection.R
, R/dbRollback_PqConnection.R
, and 1 more
postgres-transactions.Rd
dbBegin()
starts a transaction. dbCommit()
and dbRollback()
end the transaction by either committing or rolling back the changes.
# S4 method for PqConnection
dbBegin(conn, ..., name = NULL)
# S4 method for PqConnection
dbCommit(conn, ..., name = NULL)
# S4 method for PqConnection
dbRollback(conn, ..., name = NULL)
a PqConnection object, produced by
DBI::dbConnect()
Unused, for extensibility.
If provided, uses the SAVEPOINT
SQL syntax
to establish, remove (commit) or undo a ßsavepoint.
A boolean, indicating success or failure.
library(DBI)
con <- dbConnect(RPostgres::Postgres())
dbWriteTable(con, "USarrests", datasets::USArrests, temporary = TRUE)
dbGetQuery(con, 'SELECT count(*) from "USarrests"')
#> count
#> 1 50
dbBegin(con)
dbExecute(con, 'DELETE from "USarrests" WHERE "Murder" > 1')
#> [1] 49
dbGetQuery(con, 'SELECT count(*) from "USarrests"')
#> count
#> 1 1
dbRollback(con)
# Rolling back changes leads to original count
dbGetQuery(con, 'SELECT count(*) from "USarrests"')
#> count
#> 1 50
dbRemoveTable(con, "USarrests")
dbDisconnect(con)