How to Group Statements into a Transaction in SQLite with BEGIN and COMMIT
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
BEGIN;
<statement>;
<statement>;
COMMIT;
| Part | What 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 --versionto 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:
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 first transaction inserts two notes and ends with
COMMIT. - The second inserts a third note and ends with
ROLLBACK. - The
SELECTshows which notes were kept.
Step 2: Run It
In the folder that holds the script, run:
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.
+--------+
| body |
+--------+
| first |
| 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
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting an error to undo the whole transaction | After BEGIN, a failed INSERT didn't undo the one before it, and COMMIT saved first | When a statement fails, run ROLLBACK yourself |
A second BEGIN before the first transaction ends | cannot start a transaction within a transaction | End the first one with COMMIT or ROLLBACK |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
cannot commit - no transaction is active | COMMIT without a BEGIN before it | Start the transaction with BEGIN |
cannot start a transaction within a transaction | A transaction is still open | Finish it with COMMIT or ROLLBACK first |
Parse error near line 1 of transaction.sql: table notes already exists | The script already ran on notes.db | Delete 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,ROLLBACKand how SQLite responds to errors inside a transaction. - ON CONFLICT — what SQLite undoes when a statement breaks a constraint.
