Python Classes Homework

For this assignment you will write a program that contains at least two Python classes. Your program will be able to insert user-provided data into your database, and allow the user to query the database.

See the code that I sent out from class for an example to work from. You can also take a look at the official documentation on classes:

https://docs.python.org/3/tutorial/classes.html

Requirements

For this assignment you will write a program similar to the query_dogs.py program that we looked at in class. Instead of using dogs.sqlite you will use a SQLite database of your own design which will be created by your program if it does not exist. You will write the main() portion of your program in the file insert_or_query.py, and write your classes in a separate module which you will need to create and name appropriately.

Your database must contain at least 2 tables which are linked by a foreign key. Keep it simple so you can focus more on the classes than the database design.

Write a class which interacts with your SQLite database, similar to the DogDB class. Do not create the database beforehand. If the database file does not exist when an instance of your class is created, it must create it in the initializer. You can check to see if a file exists by using os.path.isfile(filename) from the os module. You must store the database connection as an attribute of your class.

So you might have this code in your initializer:

if not os.path.isfile(filename):
    print(filename, 'does not exist, creating it now')
    self._conn = sqlite3.connect(filename)
    self._create_tables()
else:
    self._conn = sqlite3.connect(filename)

You then need to write the _create_tables() method, which I named with an underscore to indicate that this method should only be called from inside the class. If the database file already exists, it will simply connect. Otherwise it will connect and then create the tables.

All direct interactions with the database must be encapsulated in your database class. That means any functions that you write that execute SQL statements need to be member functions of your database class, and use the database connection attribute that you create in the initializer.

You will not build your database from a CSV file, you must ask the user for data to insert. Your database class must have at least one method for inserting data into the database.

Write a second class to represent an entity from your database, like the Dog class. Your database class must have at least one method that can query your database and return an instance (or list of instances) of your entity class.

In main(), prompt the user asking them if they would like to insert into the database or query the database. If inserting, ask the user to provide values which will be inserted. If querying, ask the user for a string that can be used in the query.

For example, if I expanded query_dogs.py to fulfill the requirements for this assignment, the first run might look like this:

dogs.sqlite does not exist, creating it now.
Would you like to insert or query? insert
Enter the dog's name: Fido
Enter the dog's age: 6
Enter the dog's breed: Basset Hound

Then you should be able to run it again like this:

Would you like to insert or query? query
Would you like to query by name or by breed? name
Enter a name to search for: Fido
Found 1 result:
Fido, a 6 year old Basset Hound

Again, you need to come up with your own schema involving something other than dogs, but it can follow the same structure as the two tables in dogs.sqlite if you wish.

Submission

When you have completed your program, make sure you test it again by deleting your SQLite file and running it again, to make sure it can still properly create the database from scratch if it does not already exist.

Push only insert_or_query.py and your new module, do not add an SQLite file to the repository. Your code will automatically be checked for PEP 8 violations, but correctness will be checked manually.