Phase 4: Databases & SQL for AI/ML · ~35 minutes · SQLite · Python · CSV
Why Not Just Use a File?
Two processes read stock=100, each sells a few, each writes back — and whichever finishes last wins, silently erasing the other's sale. The final number is simply wrong, with no error anywhere.
Hiring signal: Reaches for a database because of a specific, identified pain point, not as reflexive default complexity
What you will learn
- Explain what a database actually provides over flat files: concurrent access, indexing, query languages, durability
- Reproduce a real concurrent-write problem on a flat file
- Show how a real database transaction avoids that same problem
- Decide, for a given scenario, whether it actually needs a database yet
Introduction
Type: Learn Languages: Python, SQLite Prerequisites: Phase 03 (How the Internet & APIs Actually Work) Time: ~35 minutes
Objective
Learning objectives
- Explain what a database actually provides over flat files: concurrent access, indexing, query languages, durability
- Reproduce a real concurrent-write problem on a flat file
- Show how a real database transaction avoids that same problem
- Decide, for a given scenario, whether it actually needs a database yet
What you're building
- Reproduce this lesson's lost-update race for real: a shared CSV/inventory file, two Python processes reading, modifying, and writing back concurrently, with a deliberate delay to make the race reliably reproducible
- Confirm the final value is measurably wrong (record the actual numbers: starting value, both changes applied, expected correct result, actual observed result)
- Rebuild the same scenario using SQLite with a real transaction (
BEGIN IMMEDIATE or equivalent), and confirm the final value is now correct - Write
database_decision.txt describing one real scenario of your own invention that genuinely needs a database (per the signals table) and one that's still genuinely fine as a plain file, with justification for each
A student runs a personal script once a week to append that week's expenses to a CSV file, always run by hand, one at a time, never concurrently with anything else. Does this scenario need a database yet?
Applying this lesson's actual decision signals rather than a blanket rule: single process, run by hand, never concurrent with itself or anything else — the lost-update race this lesson demonstrated categorically cannot happen here, since there's only ever one writer at a time. No complex querying is described either. This is exactly the 'a file is still fine' case this lesson opened by insisting is real and legitimate — reaching for a database here would be solving a problem this scenario doesn't actually have.
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Problem, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy