SQLite for everyday data
sqlite3 is a complete SQL database in the standard library. A database is a single file, or lives in memory, and there is no server to run. Reach for it when data outgrows a JSON file: app state, caches, results you want to query.
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE notes (text TEXT)")
add = "INSERT INTO notes VALUES (?)"
with db:
db.execute(add, ("it's done",))
db.executemany(add, [("a",), ("b",)])
rows = db.execute("SELECT text FROM notes")
print(rows.fetchall())- Pass values through
?placeholders, never by formatting them into the SQL text. Then a quote inside a value cannot break the statement or inject SQL of its own. with db:commits at the end of the block, or rolls back if it raises.executemanyruns one statement for each row in a list.sqlite3.connect("app.db")uses a file, created if it does not exist.
Let SQL do the work
GROUP BY, SUM and ORDER BY run inside SQLite, next to the data, so a report is one query rather than a Python loop.
Your task
Write load_orders(orders) that creates an in-memory database with a table orders(customer TEXT, amount REAL), inserts the (customer, amount) pairs with executemany and placeholders, and returns the connection. Write totals(db) returning (customer, total) tuples from one query, largest total first and ties in name order.
The explanation opens here once every check passes.