Python + SQL
Every Python job touches a database. The standard library ships sqlite3, a complete SQL engine in one file — and its API (connect → cursor → execute → fetch) is the same DB-API every other driver follows, so what you learn here transfers to Postgres, MySQL and Snowflake. This one runs right here.
You should see
cy 210.0
ada 120.0
bob 35.5
3 orders over 50
orders table still exists: 4 rowscreated column, insert dates as ISO strings, and select the orders from a given month with a parameter.The same code against PostgreSQL
Swap the driver, keep the shape. psycopg (v3) is the standard Postgres driver; parameters are %s instead of ?, and a connection is a context manager that commits on success and rolls back on error.
import psycopg # pip install "psycopg[binary]"
with psycopg.connect("postgresql://app:secret@localhost:5432/shop") as con:
with con.cursor() as cur:
cur.execute(
"SELECT customer, SUM(total) FROM orders WHERE status = %s GROUP BY customer",
("paid",),
)
for customer, spent in cur.fetchall():
print(customer, spent)
# leaving the block commits; an exception rolls back