SQLite is a complete SQL database that ships with Python — no server to install, the whole database is a single file. It's perfect for local apps, prototypes, tests, and even production for modest workloads. Learning it teaches the database pattern you'll reuse everywhere.
Connect, cursor, commit
The pattern: connect to get a connection, get a cursor to run SQL, and commit() to save changes:
import sqlite3conn = sqlite3.connect("app.db")cur = conn.cursor()cur.execute("""CREATE TABLE IF NOT EXISTS students(id INTEGER PRIMARY KEY, name TEXT, package REAL)""")cur.execute("INSERT INTO students(name, package) VALUES (?, ?)", ("Aanya", 7.5))conn.commit() # persist the changefor row in cur.execute("SELECT * FROM students WHERE package > ?", (7,)):print(row)conn.close()
Always parameterise
# UNSAFE — never do thiscur.execute(f"SELECT * FROM users WHERE name = '{name}'")# SAFE — the value can never be executed as SQLcur.execute("SELECT * FROM users WHERE name = ?", (name,))
Key takeaways
- 01sqlite3 is built in — a full SQL database in a single file.
- 02Connect → cursor → execute SQL → commit() to save → close.
- 03Always use ? placeholders for values to prevent SQL injection.
- 04Ideal for local apps, tests, prototypes and modest production use.
- 05Use the connection as a context manager for automatic commit/rollback.
