BeginnerCoder

Lesson 13 — BEGIN / COMMIT by hand

BEGIN / COMMIT / ROLLBACK by hand

You can also control transactions with SQL directly. BEGIN starts one, COMMIT makes the changes permanent, ROLLBACK throws them away. Opening the connection with isolation_level=None tells Python not to start transactions for you, so your own BEGIN is in charge.

conn.execute("BEGIN")
conn.execute("DELETE ...")
conn.execute("ROLLBACK")   # as if the DELETE never happened

Why it matters: in many tools (database consoles, other languages) you write BEGIN / COMMIT yourself — and you need ROLLBACK when a check fails halfway.

Want it another way?
Task

After the check fails, run ROLLBACK instead of COMMIT. Expected: stock stays 5.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.