NCEA Compatibility
Module: as91906 Use complex programming techniques to develop a computer program
Requirements: reading from, or writing to, files or other persistent storage
Go to topModule: as91906 Use complex programming techniques to develop a computer program
Requirements: reading from, or writing to, files or other persistent storage
Go to topOn completion of this section you will know how to:
In the previous section we looked at storing data in text files. This technique is simple but the amount of processing available on the stored data is very limited and even that processing has to be individually coded.
Databases on the other hand allow us much greater scope for processing the stored data, especially if we are familiar with SQL. We can retrieve records based on a person’s name, the date of the transaction or any other criteria we wish. We can also update a number of records simultaneously or remove records that are no longer needed. Also it is easy to interface the database with other applications.
Go to topIn order to work with a database the first thing we must do is to actually create one. Listing 1 below shows us how to do that.
Line 2 imports the module sqlite3. This is a module that comes with the Python system and it contains routines for processing a database.
Line 3 uses the connect() function from sqlite3 to create or open a database named “Workers.db”. Although it looks very different its function is quite similar to the command for opening a text file that we encountered in the previous chapter, i.e. myfile=open(“Workers.txt”,”a”). In both cases a text file or a database is opened if it already exists and is created if it does not exist. This, however, is where any similarities to text files end.
A database is a far more sophisticated data storage than a text file and opening it is not enough for storing data into it. To begin with we will have two pointers for working with the database, one for controlling the database itself and another for controlling its contents, which in our case means its tables.
The pointer for controlling the database itself is called the connection and at line 3 the variable storing it is also called connection.
The pointer for controlling the contents of the database is called the cursor and at line 4 a pointer to it is created, also called cursor.
As stated before processing the database is done by running SQL statements. In our case we use the execute() function of the cursor to run those SQL statements. We see an example of this in line 5 where we use the create table SQL statement to create a table named workers which has fields named emplname, hours, rate, gross, tax, SuperContribution and net. Apart from the first field, which is of type text, all the other fields are of type real, i.e. they can hold floating point numbers.
By the time line 5 is executed a table is defined for the database but may still be held in the computer’s memory instead of written to the database file itself. To ensure that it is written to the file we use the connection.commit() as shown in line 6.
As our table is already created, we don’t need our cursor any longer so we close it at line 7.
Go to topLines 1 to 15 of Listing 2 are identical to their equivalents in the two previous chapters and we shall therefore concentrate on the output section which spans lines 18 - 25.
Lines 18 and 19 open the database and establish a connection in almost the same way as in Listing 1. The only difference here is that since the database “Workers.db” was already existing, no new database was created – only the existing one was opened.
Next we have to create an SQL statement in order to store the payroll details in the table workers. The SQL statement is crated at lines 20 and 21.
Let us look at how the statement is created.
We shall first assume that during the input stage the user entered the following data: Tom Thumb for the employee’s name and 20, 30 respectively for the hours and rate. Thus, after processing is complete, the variables we wish to store in the database have values as shown in the table below.
Variable | Value |
---|---|
strName | Tom Thumb |
floatHours | 20 |
floatRate | 30 |
floatGross | 600 |
floatTax | 158 |
floatNet | 442 |
The SQL statement must therefore look as follows:
insert into workers values('Tom Thumb', 20, 30.0, 600.0, 158.0, 442.0)
Line 20, by mixing string constants and the variables strName, floatHours and floatRate together creates the first part of the SQL statement, i.e.
insert into workers values('Tom Thumb', 20.0, 30.0
while line 21 adds the following to it:
, 600.0, 158.0, 442.0)
Thus the two together create a complete SQL statement.
Line 22 prints out the newly created SQL statement. This is purely for our reference only so we can double check that a properly structured statement was created at lines 20 and 21, and in a real life application would not normally occur.
Line 23 runs the SQL statement in the database which means creating a new record and adding the data shown in table 1 above to its fields.
Line 24 ensures that the data is written to the physical file on the disk and line 25 closes the cursor.
Figure 1 above shows a run-through of our application.
Go to topListing 3 above is purely for reading the contents of the database, or more precisely the contents of the table workers within that database.
Lines 2, 3 and 4 work in the exact same way as their equivalents do in Listing 1 and Listing 2, so we shall leave them and concentrate on the rest of the code especially lines 5 to 9.
Line 5 runs an SQL query - Select * from workers
- on the database. This is much shorter than any of the previous one and therefore needs an explanation. Firstly the keyword select means that we are reading data from the database not adding or modifying the data that is there. The ‘*’ means that we will read every field in the record. This means that the short SQL statement above is equivalent to Select emplname, hours, rate, gross, tax, net from workers
. The outcome is that the system will read every field of every record in the database.
Line 6 calls the fetchall() function to retrieve the selected data and store it in the variable result. If the database table workers has four records in it result will have a list with four items in the list. Of course since each of the records in the database has seven fields in it, each one of the four items of our list is a list in its own right with seven items in it.
Lines 7 to 9 is a for loop. The loop variable rec starts with pointing to the first item, i.e. the first record in the list.
Line 8 reads the seven fields in rec and stores then in the variables emplname, hours, rate, gross, tax, and net.
Line 9 simply prints each of the above variables.
Lines 7 to 9 are repeated for every other record in rec and when all records are processed control goes to line 10 where the close() function closes the database connection.
Fig 2 below shows the result of running this code.
Go to topDatabases comprise a more flexible way of storing data as opposed to text files. As well as creating a database you must create a structured table inside the database in which to store your data. The table is created using the CREATE TABLE SQL statement.
Data is entered into the database using the INSERT INTO SQL statement. Data is retrieved from the database using the SELECT SQL statement.
Go to topCopy listing 1 into an editor and save it as a Python file. Next run it and check that a database is actually created.
Next copy Listing 2 into an editor then save and run it. Enter at least one record into the database.
Finally copy Listing 3 and save and run it like the others. It should display the results of the data you have just entered.
Go to topModify the assignment you created for Assignment Part 10 so that instead of storing data in a text file it will store it in a table within a database. This means that you will be dealing with three applications: