PythiloLesson 8 of 8 · progress kept in this browser

Make a transfer all-or-nothing

A balance transfer changes two rows that must stay consistent. A database transaction groups them into one commit, and any validation failure should roll everything back. SQLite's connection context manager commits on success and rolls back on an exception. Validate the destination as well as the source before treating the operation as complete.

import sqlite3
con = sqlite3.connect(":memory:")
with con:
    con.execute("CREATE TABLE sample(x)")
Your task

Write transfer(connection, source, target, amount). accounts(id TEXT PRIMARY KEY, balance INTEGER) exists with no pending transaction. Reject amount <= 0, missing accounts, or insufficient funds with ValueError. Move funds atomically, leaving balances unchanged on rejection. A valid same-account transfer is a no-op.

All lessons