Home / Your Stack / SQLite Queries
Lesson 3 of 4

SQLite Queries

Concept

SQLite is a database stored as a single file. You talk to it with SQL — a language for asking questions about data. SELECT reads, INSERT creates, UPDATE changes, DELETE removes.
m3shdup/app/db.py
# Read: get all online agents
SELECT * FROM agents WHERE status = 'online'

# Read with sorting: agents by workload
SELECT id, active_tasks, max_concurrent
FROM agents
WHERE status IN ('online', 'busy')
ORDER BY (CAST(active_tasks AS REAL) / max_concurrent) ASC

# Write: update an agent's task count
UPDATE agents SET active_tasks = active_tasks + 1 WHERE id = 'rex'

What's happening

SELECT = what columns. FROM = which table. WHERE = filter rows. ORDER BY = sort results. The second query is from your dispatch system — it finds agents sorted by how busy they are (workload ratio, lowest first).

Quick Check — 3 questions

1. What does SELECT * FROM agents return?

2. What does WHERE status = 'online' do?

3. What SQL keyword creates new rows?