Skip to main content
Albero Academy

SQLite

Python's built-in, zero-setup SQL database.

Databases10 min

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:

sqlite3
import sqlite3
conn = 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 change
for row in cur.execute("SELECT * FROM students WHERE package > ?", (7,)):
print(row)
conn.close()

Always parameterise

safe vs unsafe
# UNSAFE — never do this
cur.execute(f"SELECT * FROM users WHERE name = '{name}'")
# SAFE — the value can never be executed as SQL
cur.execute("SELECT * FROM users WHERE name = ?", (name,))

Key takeaways

  1. 01sqlite3 is built in — a full SQL database in a single file.
  2. 02Connect → cursor → execute SQL → commit() to save → close.
  3. 03Always use ? placeholders for values to prevent SQL injection.
  4. 04Ideal for local apps, tests, prototypes and modest production use.
  5. 05Use the connection as a context manager for automatic commit/rollback.

Finished this chapter?

Mark it complete to track your progress through Python.