Module 20 · Building Real Python Applications — topic 8 of 13
Data persistence
Keeping application data between runs: choosing between text files, CSV, JSON and SQLite, loading on start-up and saving on change, starting with empty data when nothing is saved yet, and a first look at sqlite3.
Learning objectives
- Choose a storage format for an application
- Load data at start-up and save it on change
- Store and query data with sqlite3
Definition
Data persistence means storing an application's data outside memory — in files or databases — so it survives after the program ends and can be loaded the next time it starts.
Introduction
An expense tracker that forgets everything when closed isn't useful. Persistence is what turns a script into an application.
Why it matters
Choosing the right storage keeps the application simple now and lets it grow later without rewriting everything.
Detailed explanation
Plain text and CSV files suit simple lists of records and are easy to open in other programs.
JSON suits nested data (records with lists and sub-objects) and maps directly to Python dictionaries and lists.
SQLite (the built-in sqlite3 module) is a full SQL database in a single file: good for larger data, searching and reports.
Load data once at start-up into memory; save after every change, or at clear points such as quitting.
Handle the first run: if the file doesn't exist yet, start with empty data instead of crashing.
Keep storage code in one place (a storage module or repository class) so the format can change without touching the rest of the program.
With sqlite3, always use ? placeholders for values (never string formatting) to avoid SQL injection, and commit changes.
Back up or version important data; storage bugs can destroy it.
Syntax
- Files for simple data, SQLite for more.
- Load on start, save on change.
- Use ? placeholders.
Code examples
Saving and loading a text file
[] [(250.0, 'food'), (40.0, 'travel')]
A SQLite database
[('food', 370.0), ('travel', 40.0)]More examples
Proving data survives
2 {'amount': 40, 'category': 'travel'}
1Step-by-step explanation
- JsonStore loads existing data in __init__ and saves after every add.
- Creating a second store from the same file simulates restarting the program; the data is still there.
- The same is true for SQLite: a new connection sees the committed row.
Expected output
Running “Proving data survives” prints exactly:
Practical applications
- Desktop apps saving settings and documents.
- Mobile apps and browsers using SQLite internally.
Best practices
- Keep storage behind one interface.
- Handle the first run gracefully.
- Use parameterised SQL and commit.
Common mistakes
Building SQL with f-strings
Avoid
Prefer
Formatted SQL breaks on quotes and allows SQL injection.
Forgetting to commit
Avoid
Prefer
Uncommitted changes are discarded.
Important notes
- The next lesson looks at JSON storage in more detail, including versioning and corrupt files.
Key takeaways
- Persistence keeps data between runs.
- Text/CSV, JSON or SQLite depending on the data.
- Load at start, save on change, handle the first run.
Try it yourself
- 1Store expenses in SQLite and print a total per month.
- 2Switch the JsonStore example to CSV without changing the code that uses it.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedWhy use ? placeholders in sqlite3 queries instead of f-strings?