Someone was in the server room.
The doors remember.
Last week: an admin login at 03:47. This week: the badge readers on every door of HQ, stored in a real database. Ask it questions in SQL, find out who it was, then break into a vault that was built badly and fix it.
> boot sequence
- Download
Click the green button. The zip lands in
Downloads. - Extract
Right-click the zip → Extract All… Don't work inside the zip!
- Open folder
VS Code → File → Open Folder… → pick the
case-archivefolder itself, not the one around it.case.dbmust be in the list on the left. - Look first
Open
case.dbin SQLite Viewer. Click through the 4 tables. That's what your code will talk to. - Run ▶
Open
00_look.pyand press the triangle in the top right. Answers appear in the terminal at the bottom.
> files
> cheat sheet
con = sqlite3.connect("case.db") | open the database |
con.execute("SQL").fetchall() | run SQL, get a list of tuples |
….fetchone() | just the first row, or None |
SELECT * FROM agents | every column, every row |
SELECT name, clearance FROM agents | only these columns |
WHERE clearance >= 4 | only matching rows. Text: room = 'lab' |
ORDER BY score DESC | sort, biggest first (leave out DESC for smallest first) |
LIMIT 5 | only the first 5 rows |
WHERE codename = ?", (c,) | put a value in safely. One value needs the comma! |
INSERT INTO scores VALUES (?, ?) | add a row |
con.commit() | save. Without it, INSERT is forgotten |
for name, score in rows: | unpack each tuple into variables |
> something broke
no such table: agents
Python didn't find case.db, so it quietly made a new, empty one. Did you open the case-archive folder itself in VS Code? Is the name spelled right? Delete the empty case.db it made somewhere else.
UNIQUE constraint failed
That codename is already in the table. A PRIMARY KEY can only exist once. Pick another one.
I INSERTed but it's not in the viewer
Did you con.commit()? Without it nothing is saved. Then refresh the viewer (close and reopen case.db).
Incorrect number of bindings supplied
The number of ? and the number of values must match. One value? Write the comma: (team,), not (team).
syntax error near …
The SQL itself is wrong. Text needs 'quotes', check the spelling of SELECT, FROM, WHERE, and that the order is SELECT → FROM → WHERE → ORDER BY → LIMIT.
database is locked
Another program is writing to the file, maybe the viewer with unsaved edits, or an old run still waiting for input(). Close it and run again.
I broke the database
No problem: extract the zip again and you get the original case.db and vault.db back.
A red error in the terminal
Read the last line first: it says what went wrong. The line above it says which line of your file. Ctrl+click the file name to jump there.