Chaptered Walkthrough: CRUD With a Tiny To-Do List
This is a written walkthrough organised like the chapters of a short video, with no video attached. Over six chapters we build a command-line to-do list that can create, read, update and delete tasks, using Python and SQLite.
Chapter 1: the table
import sqlite3
conn = sqlite3.connect("todo.db")
conn.execute("""
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
done INTEGER NOT NULL DEFAULT 0
)
""")
One table, three columns. done uses 0 for "not done" and 1 for "done", the usual approach in SQLite.
Chapter 2: Create
def add_task(title):
with conn:
conn.execute("INSERT INTO tasks (title) VALUES (?)", (title,))
The ? placeholder keeps the title as data, whatever it contains. with conn: commits the change automatically when the block ends without an error.
Chapter 3: Read
def list_tasks():
rows = conn.execute("SELECT id, title, done FROM tasks ORDER BY id").fetchall()
for task_id, title, done in rows:
mark = "x" if done else " "
print(f"[{mark}] {task_id}: {title}")
fetchall() returns all rows as a list of tuples, which we unpack into three names. Printing with an f-string is fine here, because we are formatting output, not building SQL.
Chapter 4: Update
def complete_task(task_id):
with conn:
cur = conn.execute("UPDATE tasks SET done = 1 WHERE id = ?", (task_id,))
if cur.rowcount == 0:
print("No task with that id.")
The WHERE clause limits the change to one task. rowcount tells us how many rows changed, so we can warn about a mistyped id instead of failing silently.
Chapter 5: Delete
def delete_task(task_id):
with conn:
conn.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
Again the WHERE clause is essential. Without it, every task would be deleted. A friendly app might ask "Are you sure?" before running this.
Chapter 6: putting it together
import sys
command = sys.argv[1] if len(sys.argv) > 1 else "list"
if command == "add":
add_task(" ".join(sys.argv[2:]))
elif command == "done":
complete_task(int(sys.argv[2]))
elif command == "delete":
delete_task(int(sys.argv[2]))
list_tasks()
Try it:
python todo.py add Buy milk
python todo.py add Call grandma
python todo.py done 1
python todo.py delete 2
After each command the list is printed, so you can watch the table change.
What each chapter taught
| Chapter | CRUD letter | SQL | Key habit |
|---|---|---|---|
| 2 | C | INSERT | Use placeholders |
| 3 | R | SELECT | Always choose an order |
| 4 | U | UPDATE | Always use WHERE; check rowcount |
| 5 | D | DELETE | Always use WHERE; consider confirming |
Ideas to extend it
- add a
duedate column and list overdue tasks first - add an "undo" by marking tasks deleted instead of removing them
- move the code into a small web app
For the ideas behind each operation, read the long read on create, read, update and delete.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
Illustrative outline only: no live session, recording, registration or certificate is offered on this site. Any speakers, panels or schedules mentioned are hypothetical.