Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Files and data on a server

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

60 minutes

Where does your high score live?

You score 4,200 in a game. You close the app. You open it tomorrow. The score is still there.

A program forgets everything when it stops. So the score must be saved somewhere: in a file or in a database (an organised store of data).

Today you learn both.

Key words

WordWhat it means
FileA named store of data on a disk.
OpenAsk the computer to get a file ready for reading or writing.
CloseTell the computer you finished. It saves the data and frees the file.
Local fileA file on the same computer that runs the program. A file on a server works the same way in code.
ModeWhat you plan to do with a file: read, write or append.
DatabaseAn organised store of data that programs can search and change.
TableA grid in a database. Each row is one record and each column is one fact.
RowOne record in a table, such as one player.
PlaceholderA ? in an SQL line. It marks where a value goes in safely.
SQLThe language for asking a database for data.

The cells students fill in are left blank in this preview.

The file modes

ModeMeaningIf the file existsIf it does not
"r"ReadReads itError
"w"WriteDeletes what was thereCreates it
"a"AppendAdds to the endCreates it

The cells students fill in are left blank in this preview.

Try it: write, read, delete

Code: python
import os, tempfile

folder = tempfile.mkdtemp()
path = os.path.join(folder, "scores.txt")

with open(path, "w") as handle:      # with closes the file for you
    handle.write("Sam 10\n")

with open(path, "a") as handle:
    handle.write("Aisha 7\n")

with open(path, "r") as handle:
    print(handle.read())

os.remove(path)                      # delete the file
print(os.path.exists(path))

Quick check: which mode?

Have a go

Quick check: which mode?

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Use "with"

data-callouts.tip.label

The with statement closes the file for you, even if there is an error. It is the safe way. Without it, you must remember to close.

Files are not enough

A file is fine for a short list. For many users, many fields and fast search, use a database.

You change data with four jobs: add a row (INSERT), read rows (SELECT), modify a row (UPDATE) and delete a row (DELETE).

Four jobs, four SQL words

JobSQLExample
AddINSERTINSERT INTO scores VALUES ('Sam', 10)
ReadSELECTSELECT * FROM scores
ModifyUPDATEUPDATE scores SET points = 12 WHERE name = 'Sam'
DeleteDELETEDELETE FROM scores WHERE name = 'Sam'

The cells students fill in are left blank in this preview.

Try it: change data with SQL

Code: sql
UPDATE scores SET points = 12 WHERE name = 'Sam';
DELETE FROM scores WHERE name = 'Ben';
SELECT * FROM scores;

-- What happens if you forget WHERE on the DELETE?

The lesson goes on

7 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.