{"id":52439,"date":"2019-07-09T13:31:32","date_gmt":"2019-07-09T12:31:32","guid":{"rendered":"https:\/\/www.raspberrypi.org\/?p=52439"},"modified":"2021-09-19T16:46:26","modified_gmt":"2021-09-19T15:46:26","slug":"how-to-build-databases-using-python-and-text-files-hello-world-9","status":"publish","type":"post","link":"https:\/\/www.raspberrypi.org\/blog\/how-to-build-databases-using-python-and-text-files-hello-world-9\/","title":{"rendered":"How to build databases using Python and text files | Hello World #9"},"content":{"rendered":"

In Hello World issue 9, Raspberry Pi’s own\u00a0Mac Bowley shares a lesson that introduces students to databases using Python and text files.<\/p>\n

\"\"<\/a><\/p>\n

In this lesson, students create a library app for their books. This will store information about their book collection and allow them to display, manipulate, and search their collection. You will show students how to use text files in their programs that act as a database.<\/p>\n

The project will give your students practical examples of database terminology and hands-on experience working with persistent data. It gives opportunities for students to define and gain concrete experience with key database concepts using a language they are familiar with. The script that accompanies this activity can be adapted to suit your students\u2019 experience and competency.<\/p>\n

This ready-to-go software project can be used alongside approaches such as PRIMM<\/a> or pair programming, or as a worked example to engage your students in programming with persistent data.<\/p>\n

What makes a database?<\/h2>\n

Start by asking the students why we need databases and what they are: do they ever feel unorganised? Life can get complicated, and there is so much to keep track of, the raw data required can be overwhelming. How can we use computing to solve this problem? If only there was a way of organising and accessing data that would let us get it out of our head. Databases are a way of organising the data we care about, so that we can easily access it and use it to make our lives easier.<\/p>\n

\"\"<\/a><\/p>\n

Then explain that in this lesson the students will create a database, using Python and a text file. The example I show students is a personal library app that keeps track of which books I own and where I keep them. I have also run this lesson and allowed the students pick their own items to keep track of \u2014 it just involves a little more planning time at the end. Split the class up into pairs; have each of them discuss and select five pieces of data about a book (or their own item) they would like to track in a database. They should also consider which type of data each of them is. Give them five minutes to discuss and select some data to track.<\/p>\n

Databases are organised collections of data, and this allows them to be displayed, maintained, and searched easily. Our database will have one table \u2014 effectively just like a spreadsheet table. The headings on each of the columns are the fields: the individual pieces of data we want to store about the books in our collection. The information about a single book are called its attributes and are stored together in one record, which would be a single row in our database table. To make it easier to search and sort our database, we should also select a primary key: one field that will be unique for each book. Sometimes one of the fields we are already storing works for this purpose; if not, then the database will create an ID number that it uses to uniquely identify each record.<\/p><\/blockquote>\n

Create a library application<\/h2>\n

Pull the class back together and ask a few groups about the data they selected to track. Make sure they have chosen appropriate data types. Ask some if they can find any of the fields that would be a primary key; the answer will most likely be no. The ISBN could work, but for our simple application, having to type in a 10- or 13-digit number just to use for an ID would be overkill. In our database, we are going to generate our own IDs.<\/p>\n

The requirements for our database are that it can do the following things: save data to a file, read data from that file, create new books, display our full database, allow the user to enter a search term, and display a list of relevant results based on that term. We can decompose the problem into the following steps:<\/p>\n