Python For GCSE
Making A Simple Database

To start this off, you need to make yourself a text file containing some film data. I started with two films, taking the information from imdb.com. The file is saved with the name films.csv

Star Wars,1977,George Lucas,8.6
Twilight,2008,Catherine Hardwicke,5.2

CSV stands for comma separated variable. This is a standard basic text format for storing data. Don't use any spaces in your file, just do line breaks after each row.

The columns are, Title, Year, Director, Rating

Your job is to create a Python program that uses a CSV file and acts as a database management system.

This program has an outline menu and some functionality already created for you. The tasks are explained in order in the form of comments.

# data taken from imdb.com

# reading a file
def read_file():
    # open the file
    thefile = open("films.csv","r")
    # read the whole file into the list
    rows = [line for line in thefile]
    # close the file
    thefile.close()
    # remove the line break at the end of each list item
    for i,r in enumerate(rows):
        rows[i] = r.replace("\n","")
    return rows

# procedure to show all of the records (for testing)
def show_all():
    all = read_file()
    for i,r in enumerate(all):
        print(i,r)
    
# main menu
def main_menu():
    print("Database Main Menu")
    print("******************")
    print()
    menuitems = ["Add","Edit","Delete","Search","Show All", "Quit"]
    for i,m in enumerate(menuitems):
        print(chr(i+97),m)
    print()
    chosen = input("Enter a choice: ")
    return chosen

# Task 1 - Adding Records
def add_record():
    # open the file in append mode
    # get the user to input the individual parts of a record 
    # form a string of those parts using commas between 
    # (this and last step could be done as a single separate function)
    # write a line break to the file
    # write a record to the file
    print("You haven't written this yet.")

# Task 2 - Deleting Records
def delete_record():
    # call the show_all procedure - it lists the records in the file and allocates a number
    # get the user to input the number of the row to delete
    # use the read_file function to get a list - del listname[index of item to delete]
    # open the file in write mode
    # write the list to the file, line break after each item
    print("You haven't written this yet.")

# Task 3 - Editing Records
def edit_record():
    # call the show_all procedure - it lists the records in the file and allocates a number
    # get the user to input the number of the row to edit
    # use the read_file function to get a list
    # get the user to input the new values for the row
    # form a string of those parts with commas between
    # (this and last step could be done as a separate function - or use the function from before)
    # open the file in write mode
    # write the list to the file, line break after each item
    print("You haven't written this yet.")

# Task 4 - Searching
def search():
    # Write a submenu for different ways to search
    # by first letter, by director, by year, by rating
    # Write individual functions that do those jobs for you.
    print("You haven't written this yet.")

# main program
c = ""
while c != "f":
    c = main_menu()
    print()
    if c=="a":
        print("Add A Record")
        print()
        add_record()
        print()
    elif c=="b":
        print("Edit A Record")
        print()
        edit_record()
        print()
    elif c=="c":
        print("Delete A Record")
        print()
        delete_record()
        print()
    elif c=="d":
        print("Search")
        print()
        search()
        print()
    elif c=="e":
        print("All Records")
        print()
        show_all()
        print()

You may need to use other references to assist you and it might help you to refer back to programs that you have done in the past, where you have used these techniques. The functions of a database management system that you will replicate are,

  • Inserting/Adding Records
  • Deleting Records
  • Editing Records

There is also an expectation of being able to search the file in useful, predictable ways.

Extension

Records in the file are in the order they are entered. When the file is displayed, it should be possible to view them in alphabetical order by film. Since the film title comes first in the record, this is quite easy to do.

Improve the search tools so that the user of your program can add different types of constraints for each field and create complex queries.

Add validation to your program to prevent errors due to invalid input from the user.