Difference between revisions of "Database.Rollback"

From Xojo Documentation

m
Line 13: Line 13:
  
 
You have to have an open transaction to be able to use Rollback. On SQLite (and other databases), you can start a transaction with this command:
 
You have to have an open transaction to be able to use Rollback. On SQLite (and other databases), you can start a transaction with this command:
 
+
<pre class="code">BEGIN TRANSACTION</pre>
  BEGIN TRANSACTION
 
  
 
It can be sent using SQLExecute:
 
It can be sent using SQLExecute:
 
 
<rbcode>
 
<rbcode>
 
DB.SQLExecute("BEGIN TRANSACTION")
 
DB.SQLExecute("BEGIN TRANSACTION")
 
</rbcode>
 
</rbcode>
  
==Example==
+
== Sample Code ==
 
+
This code uses rollback to revert database changes in a transaction when an error occurs:
This example uses rollback to revert database changes in a transaction when an error occurs:
 
 
 
 
<rbcode>
 
<rbcode>
 
// Prior DB code has run
 
// Prior DB code has run

Revision as of 21:20, 23 January 2019

Method

Database.Rollback()

Supported for all project types and targets.

Cancels an open transaction restoring the database to the state it was in before the transaction began.

Notes

You will generally want to rollback database changes if a database error occurs within the transaction.

You have to have an open transaction to be able to use Rollback. On SQLite (and other databases), you can start a transaction with this command:

BEGIN TRANSACTION

It can be sent using SQLExecute:

DB.SQLExecute("BEGIN TRANSACTION")

Sample Code

This code uses rollback to revert database changes in a transaction when an error occurs:

// Prior DB code has run

If Not DB.Error Then
DB.Commit
Else
MsgBox("Error: " + DB.ErrorMessage)
DB.Rollback
End If

See Also

Database.Close, Database.Commit