Small. Fast. Reliable.
Choose any three.
Search for:

SQL As Understood By SQLite

[Top]

BEGIN TRANSACTION

begin-stmt:

R-49287-50618:[ ]

commit-stmt:

R-55215-14389:[ ]

rollback-stmt:

R-51224-47395:[ ]

R-45327-47888:[No changes can be made to the database except within a transaction. ] R-41155-30776:[Any command that changes the database (basically, any SQL command other than SELECT) will automatically start a transaction if one is not already in effect. ] R-04527-41458:[Automatically started transactions are committed when the last query finishes. ]

R-36570-50350:[Transactions can be started manually using the BEGIN command. ] R-29897-28851:[Such transactions usually persist until the next COMMIT or ROLLBACK command. But a transaction will also ROLLBACK if the database is closed or if an error occurs and the ROLLBACK conflict resolution algorithm is specified. ] See the documentation on the ON CONFLICT clause for additional information about the ROLLBACK conflict resolution algorithm.

R-11129-23371:[END TRANSACTION is an alias for COMMIT. ]

R-02854-44844:[Transactions created using BEGIN...COMMIT do not nest. ] R-50442-34254:[For nested transactions, use the SAVEPOINT and RELEASE commands. ] The "TO SAVEPOINT name" clause of the ROLLBACK command shown in the syntax diagram above is only applicable to SAVEPOINT transactions. R-11576-11990:[An attempt to invoke the BEGIN command within a transaction will fail with an error, regardless of whether the transaction was started by SAVEPOINT or a prior BEGIN. ] R-58433-37187:[The COMMIT command and the ROLLBACK command without the TO clause work the same on SAVEPOINT transactions as they do with transactions started by BEGIN. ]

R-22212-63356:[Transactions can be deferred, immediate, or exclusive. ] R-41163-31141:[The default transaction behavior is deferred. ] R-47211-28729:[Deferred means that no locks are acquired on the database until the database is first accessed. ] R-24949-48633:[Thus with a deferred transaction, the BEGIN statement itself does nothing to the filesystem. ] R-09261-30775:[Locks are not acquired until the first read or write operation. ] R-45406-28279:[The first read operation against a database creates a SHARED lock and the first write operation creates a RESERVED lock. ] R-25508-47517:[Because the acquisition of locks is deferred until they are needed, it is possible that another thread or process could create a separate transaction and write to the database after the BEGIN on the current thread has executed. ] R-32369-26304:[If the transaction is immediate, then RESERVED locks are acquired on all databases as soon as the BEGIN command is executed, without waiting for the database to be used. ] R-50920-12644:[After a BEGIN IMMEDIATE, no other database connection will be able to write to the database or do a BEGIN IMMEDIATE or BEGIN EXCLUSIVE. ] R-28051-59567:[Other processes can continue to read from the database, however. ] R-26176-60647:[An exclusive transaction causes EXCLUSIVE locks to be acquired on all databases. ] R-13825-09904:[After a BEGIN EXCLUSIVE, no other database connection except for read_uncommitted connections will be able to read the database and no other connection without exception will be able to write the database until the transaction is complete. ]

R-38061-31091:[An implicit transaction (a transaction that is started automatically, not a transaction started by BEGIN) is committed automatically when the last active statement finishes. A statement finishes when its prepared statement is reset or finalized. An open sqlite3_blob used for incremental BLOB I/O counts as an unfinished statement. The sqlite3_blob finishes when it is closed. ]

R-11684-02844:[The explicit COMMIT command runs immediately, even if there are pending SELECT statements. ] R-25108-07802:[However, if there are pending write operations, the COMMIT command will fail with an error code SQLITE_BUSY. ]

R-59194-45393:[An attempt to execute COMMIT might also result in an SQLITE_BUSY return code if an another thread or process has a shared lock on the database that prevented the database from being updated. ] R-55708-49653:[When COMMIT fails in this way, the transaction remains active and the COMMIT can be retried later after the reader has had a chance to clear. ]

In very old versions of SQLite (before version 3.7.11 - 2012-03-20) the ROLLBACK will fail with an error code SQLITE_BUSY if there are any pending queries. R-41051-26626:[In more recent versions of SQLite, the ROLLBACK will proceed and pending statements will often be aborted, causing them to return an SQLITE_ABORT or SQLITE_ABORT_ROLLBACK error. ] R-03042-35027:[In SQLite version 3. ] 8.8 (2015-01-16) and later, a pending read will continue functioning after the ROLLBACK as long as the ROLLBACK does not modify the database schema.

If PRAGMA journal_mode is set to OFF (thus disabling the rollback journal file) then the behavior of the ROLLBACK command is undefined.

Response To Errors Within A Transaction

R-26708-58601:[If certain kinds of errors occur within a transaction, the transaction may or may not be rolled back automatically. The errors that can cause an automatic rollback include:

]

R-15522-56042:[For all of these errors, SQLite attempts to undo just the one statement it was working on and leave changes from prior statements within the same transaction intact and continue with the transaction. ] R-57255-33789:[However, depending on the statement being evaluated and the point at which the error occurs, it might be necessary for SQLite to rollback and cancel the entire transaction. ] R-24408-11841:[An application can tell which course of action SQLite took by using the sqlite3_get_autocommit() C-language interface. ]

It is recommended that applications respond to the errors listed above by explicitly issuing a ROLLBACK command. R-39822-14939:[If the transaction has already been rolled back automatically by the error response, then the ROLLBACK command will fail with an error, but no harm is caused by this. ]

Future versions of SQLite may extend the list of errors which might cause automatic transaction rollback. Future versions of SQLite might change the error response. In particular, we may choose to simplify the interface in future versions of SQLite by causing the errors above to force an unconditional rollback.