Skip to main content

How to Group Statements into a Transaction in SQLite with BEGIN and COMMIT

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some changes belong together: they should all be saved, or none of them. A transaction groups them. BEGIN starts it, COMMIT saves every change in it, and ROLLBACK undoes them all. This guide saves one transaction and undoes another, with real output from SQLite 3.54.1.

Syntax​

Syntax
BEGIN;
<statement>;
<statement>;
COMMIT;
PartWhat it means
BEGIN;Starts a transaction
COMMIT;Saves every change made since BEGIN, all at once
ROLLBACK;Used instead of COMMIT: undoes every change made since BEGIN

SQLite's documentation on transactions: "Transactions can be started manually using the BEGIN command. Such transactions usually persist until the next COMMIT or ROLLBACK command."

Prerequisites​

  • The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run sqlite3 --version to see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.

Step 1: Write the Script​

Save this as transaction.sql:

transaction.sql
CREATE TABLE notes (
body TEXT NOT NULL
);

BEGIN;
INSERT INTO notes (body) VALUES ('first');
INSERT INTO notes (body) VALUES ('second');
COMMIT;

BEGIN;
INSERT INTO notes (body) VALUES ('third');
ROLLBACK;

SELECT * FROM notes;

The transaction.sql script in a macOS-style code window: a notes table, a transaction that inserts first and second and ends with COMMIT, a transaction that inserts third and ends with ROLLBACK, and a SELECT of every note.

  • The first transaction inserts two notes and ends with COMMIT.
  • The second inserts a third note and ends with ROLLBACK.
  • The SELECT shows which notes were kept.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table notes.db ".read transaction.sql"

sqlite3 opens the database file notes.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.

Output
+--------+
| body |
+--------+
| first |
| second |
+--------+

The output in a terminal: the notes table holds two rows, first and second.

COMMIT saved both notes of the first transaction. ROLLBACK undid the second one, so third is gone, as if its INSERT had never run.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting an error to undo the whole transactionAfter BEGIN, a failed INSERT didn't undo the one before it, and COMMIT saved firstWhen a statement fails, run ROLLBACK yourself
A second BEGIN before the first transaction endscannot start a transaction within a transactionEnd the first one with COMMIT or ROLLBACK

Troubleshooting​

SymptomLikely causeFix
cannot commit - no transaction is activeCOMMIT without a BEGIN before itStart the transaction with BEGIN
cannot start a transaction within a transactionA transaction is still openFinish it with COMMIT or ROLLBACK first
Parse error near line 1 of transaction.sql: table notes already existsThe script already ran on notes.dbDelete notes.db and run the script again

FAQ​

What happens without BEGIN?​

Each statement is a transaction of its own, saved as soon as it finishes. The documentation: "Any command that accesses the database (basically, any SQL command, except a few PRAGMA statements) will automatically start a transaction if one is not already in effect. Automatically started transactions are committed when the last SQL statement finishes."

Is END the same as COMMIT?​

Yes. A transaction ended with END; saved its row. The documentation: "END TRANSACTION is an alias for COMMIT."

Conclusion​

BEGIN and COMMIT saved first and second together, and ROLLBACK undid third before it was ever saved.

Additional Resources​

  • Transactions — BEGIN, COMMIT, ROLLBACK and how SQLite responds to errors inside a transaction.
  • ON CONFLICT — what SQLite undoes when a statement breaks a constraint.