Roll back one item without losing the batch
A batch may contain one invalid record while earlier records should still commit. A savepoint provides a nested rollback boundary inside the outer transaction. Releasing it does not mean the entire transaction is durable; the outer commit still owns that decision.
This exercise accepts positive amounts and skips invalid or duplicate identifiers. Rollback must target only the current item, so a blanket connection rollback is incorrect. The check deliberately places an invalid record between valid ones. Production systems also need an explicit result for each rejected item so silent partial success does not surprise callers.
import sqlite3
with sqlite3.connect(":memory:") as db:
db.execute("create table x(value int)")
db.execute("savepoint item")
db.execute("insert into x values(1)")
db.execute("rollback to item")
db.execute("release item")
print(db.execute("select * from x").fetchall())Review the design
The explicit BEGIN prevents the first released savepoint from becoming the outermost commit. This function owns its transaction and expects an idle connection; callers already inside a transaction need a different nesting contract.
Your task
Implement insert_valid(db,rows) for ledger(id primary key,amount check(amount>0)). Within one outer transaction use one savepoint per row, skip sqlite3.IntegrityError items, and return the number inserted.
The explanation opens here once every check passes.