[Tutor] How to effectively use a Student class with SQLite [Was Re: How to design object interactions with an SQLite db?]

boB Stepp robertvstepp at gmail.com
Thu Aug 13 21:18:53 CEST 2015


Beware!  Lengthy post!! Sign of a confused boB. ~(:>)

I believe that I understand now all of the things I want my project
(Tentatively named "Montessori Classroom Manager".) to *do*.  But I am
currently spinning my wheels on how to implement classes, SQLite, and
the kivy UI, so that they all effectively interact with each other.  I
think it is time to write some code and that I should start with the
most fundamental class to the project, the Student class.

The Student class, as I now envisage it, will have many attributes,
which seem to naturally map to a Student db table.  The first issue
that I am puzzling over is the very real possibility of duplicate
student names.  Of course, I was planning on the Student db table
having a primary key of student_id, which would be an unique
identifier for a particular student.  The problem with this is from
the user's perspective:  The teacher will naturally want to interact
with the program using the student's name, possibly even a student's
nickname.  These can be non-unique.

I like Alan's suggestion:

On Sat, Aug 1, 2015 at 12:30 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> On 01/08/15 17:34, boB Stepp wrote:

>> 1)  Create my various objects normally, but have their data attributes
>
>> fetched through some sort of db manager class I would design.
>
> Personally I tend to create a load() method that is used like a constructor
> but fetches the data from the database
>
> myObj = MyClass().load(ID)
>
> Where load() returns self if successful.
> Alternatively in Python you could define the ID as a parameter of init with
> a None default
>
> def __init__(self,att1=None,att2=SomeDefault,...,ID=None):
>     if ID
>        self.load(ID)
>     else:
>        self.att1 = att1 # etc...
>
> Its conceptually simple and gives you full control of the SQL, but things
> like inheritance can get tricky.

Both his ideas rely on using the primary key for a student to fetch
the information needed to create that particular student object, which
the user will not normally ever use directly.

So this leads me naturally to the UI design, something I would
normally mostly forget about until I had my main program logic well
worked out.  But in this project it seems to me that program logic, db
and UI are all intimately intertwined.

In terms of UI I imagine the user will have some sort of list of
student names displayed.  In a given school year in the case of two
students with identical names, I imagine the target user (my wife)
will want a student nickname displayed to differentiate between two
(or more) such students.  So she would select a student in the UI and
start doing stuff.  I assume if I have a list of student names in the
UI, then the program already created all of the student objects in the
list, so the student_id primary key for each student would be freely
available to use.  However, if the student did not exist in the list,
then a new student object would have to be created and its relevant
attributes recorded in the student db table with its newly created
student_id primary key.

So (Finally!) if I am understanding the important issues involved in
creating a viable Student class, I should be doing:

    1)  For the current school year, at program startup, the program
should retrieve all current students' attributes from the db,
instantiate each such student object, and be able to make each
available in the UI.

    2)  If a new student needs to be created by the user, then the
Student class would have an appropriate method which would allow for
data entry via the UI to both create a new student object and a new
student db table entry with its own unique student_id primary key.

    3)  The program logic would reference each student object only
with the student_id primary key uniquely associated with each such
student object.

Is this the way to approach this, or at least a good way?

I don't want to worry about actually coding the UI at this point, but
it looks like while testing things as I go along, I will need to have
at least a text simulation of a UI, so I can try selecting students,
creating students, etc.  Would the best way to go about this is to
create an old command line-style menu system, which lists options by
number, input number desired, action occurs etc.?

As always, many thanks in advance!

boB


More information about the Tutor mailing list