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.
Real-world application: 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: 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.
This lesson is free
You've read the first 2 sections. The rest covers The Problem, Check Yourself, Key Terms & Next. A free account opens all of it, with its practice questions and its lab. No card.
Create a free account — the opening phases of 29 of 35 courses, and the first lesson of every phase, are free, no credit card.
Browse all courses · View pricing · DeVenture Academy