Why not a file or a dictionary
After this: Compare a list of dictionaries with a database, and name four things the list cannot do.
On this page · 6 sections · 3 checks
You already know a way to keep a table in a program. In "Nested data" you kept rows in a list, and a list of dictionaries holds one dictionary for each student. So why does anyone need a database? This lesson asks the same question both ways, and then looks at what the list cannot do.
The students as a list
Here are the six students as a list, with one dictionarymap for each student. The loop finds the students who live in Leeds and are in year 2.
students = [ {"id": 1, "name": "Ana", "year": 2, "city": "Leeds"}, {"id": 2, "name": "Ben", "year": 1, "city": "York"}, {"id": 3, "name": "Chloe", "year": 2, "city": "Leeds"}, {"id": 4, "name": "Dev", "year": 3, "city": "Hull"}, {"id": 5, "name": "Eve", "year": 1, "city": "York"}, {"id": 6, "name": "Femi", "year": 3, "city": "Leeds"},]
found = []for s in students: if s["city"] == "Leeds" and s["year"] == 2: found.append(s["name"])print(found)['Ana', 'Chloe']
import java.util.ArrayList;import java.util.List;import java.util.Map;
public class Main { public static void main(String[] args) { List<Map<String, Object>> students = List.of( Map.of("id", 1, "name", "Ana", "year", 2, "city", "Leeds"), Map.of("id", 2, "name", "Ben", "year", 1, "city", "York"), Map.of("id", 3, "name", "Chloe", "year", 2, "city", "Leeds"), Map.of("id", 4, "name", "Dev", "year", 3, "city", "Hull"), Map.of("id", 5, "name", "Eve", "year", 1, "city", "York"), Map.of("id", 6, "name", "Femi", "year", 3, "city", "Leeds") );
ArrayList<String> found = new ArrayList<>(); for (Map<String, Object> s : students) { if (s.get("city").equals("Leeds") && s.get("year").equals(2)) { found.add((String) s.get("name")); } } System.out.println(found); }}[Ana, Chloe]
- The list holds six rows. Each row has the keys id, name, year and city, the same as the columns of the table.
- The loop looks at every row in turn and tests two things: is the city Leeds, and is the year 2?
- When both are true, it adds the name to
found. Ana and Chloe pass; the other four do not.
The same question to a database
A database answers the same question when you write it in . A question written in SQL is called a .
SELECT name FROM studentsWHERE city = 'Leeds' AND year = 2;| name |
|---|
| Ana |
| Chloe |
Read it aloud: "select the name from students where the city is Leeds and the year is 2". The answer is the same two names. Lesson 4 shows how to write a query like this yourself; for now, notice that it says what you want and not how to find it. There is no loop. The database decides how to go through the rows.
What the list cannot do
The list gave the right answer. The trouble starts when the data has to last, and when more than one program needs it.
- It is lost when the program stops. The list lives inside your running program. When the program ends or crashes, the list is gone, and every student added since it started is lost with it. You could save it to a file, as in "Input, files and the standard library", but then your code must read and write the whole file, and a crash halfway through a save can leave the file half written.
- Two programs cannot share it. A shop has two tills. If each till keeps its own list of stock, a sale on one till does not show on the other, and both can sell the last pen. If both write to one shared file, one till's save can wipe out the other's.
- It accepts anything. Nothing stops your code from adding a student with year 7, or a second student with id 3. The list holds whatever it is given.
- It reads every row to find one. To find student 3, the loop starts at the top. With six students that is quick. With six million it reads up to six million rows, every time.
The database program
A does those four jobs for you. Your program sends it a query and gets rows back. The database program:
- keeps the data in files, and writes each change so that a crash cannot leave it half done;
- lets several programs use the same data at once, without one wiping out another's changes;
- checks rules that you give each table, and refuses a row that breaks one;
- finds a row by its number without reading the whole table (lesson 3 says which number).
This learning path uses SQLite, a small database program that runs inside this page. Every query you run in this tutorial is real SQL, run by real SQLite. One difference from a real database: the page saves nothing. Every Run starts again from the same school tables, so you cannot break the examples.
Check yourself
A program starts with the six students in a list. While it runs, it adds a seventh student to the list. Then the computer is switched off, and the program is started again. How many students are in the list?
A shop has two tills, and one pen left in stock. Both tills must know how many pens are left. Which way of keeping the stock works?
Which of these jobs can a list of six students in your program do as well as a database?
Honesty notes
- Ask the same question of a list of rows (with a loop) and of a database (with an SQL query), and get the same answer: Ana and Chloe.
- Say what SQL and a query are: the language for asking a database, and one question written in it.
- Name four things a list cannot do: last when the program stops, be shared by two programs, refuse a bad row, and find one row without reading them all.
- Say what a database program does for you, and that SQLite runs inside your program while PostgreSQL and MySQL are separate programs you connect to.