Learn R Programming

RSQLite (version 1.0.0)

transactions: SQLite transaction management.

Description

By default, SQLite is in auto-commit mode. dbBegin starts a SQLite transaction and turns auto-commit off. dbCommit and dbRollback commit and rollback the transaction, respectively and turn auto-commit on.

Usage

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

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

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

Arguments

conn

a '>SQLiteConnection object, produced by dbConnect

name

Supply a name to use a named savepoint. This allows you to nest multiple transaction

Value

A boolean, indicating success or failure.

Examples

Run this code
# NOT RUN {
con <- dbConnect(SQLite(), ":memory:")
dbWriteTable(con, "arrests", datasets::USArrests)
dbGetQuery(con, "select count(*) from arrests")

dbBegin(con)
rs <- dbSendQuery(con, "DELETE from arrests WHERE Murder > 1")
dbGetRowsAffected(rs)
dbClearResult(rs)

dbGetQuery(con, "select count(*) from arrests")

dbRollback(con)
dbGetQuery(con, "select count(*) from arrests")[1, ]

dbBegin(con)
rs <- dbSendQuery(con, "DELETE FROM arrests WHERE Murder > 5")
dbClearResult(rs)
dbCommit(con)
dbGetQuery(con, "SELECT count(*) FROM arrests")[1, ]

# Named savepoints can be nested --------------------------------------------
dbBegin(con, "a")
dbBegin(con, "b")
dbRollback(con, "b")
dbCommit(con, "a")

dbDisconnect(con)
# }

Run the code above in your browser using DataLab