Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Using Databases in Python Meet Peewee Queries Are Your Friend

Jeremy Kerrigan
Jeremy Kerrigan
12,002 Points

If I change the number of points for a username, it doesn't update my database to the highest points holder.

The database holds onto the highest points holder when I created the database. When I created the database jeremy was the highest points holder. Then I changed my list of dictionaries to have steve have the highest points total. Though the database still says jeremy is the highest points holder. I saved the file and restarted the console as well. Still no fix. Thanks

from peewee import *

db = SqliteDatabase('students.db')

class Student(Model):
    username = CharField(max_length=255, unique=True)
    points = IntegerField(default=0)

    class Meta:
        database = db

students = [
   {'username': 'jeremy',
    'points': 1400},
    {'username': 'matt',
    'points': 11912},
    {'username': 'kevin',
     'points': 7363},
    {'username': 'annie',
     'points': 4079},
    {'username': 'steve',
     'points': 14717}
]        

def add_students():
    for student in students:
        try:
            Student.create(username = student['username'],
                          points = student['points'])
        except IntegrityError:
            student_record = Student.get(username = student['username'])
            student_record_points = student['points']
            student_record.save()

def top_student():
    student = Student.select().order_by(Student.points.desc()).get()
    return student

if __name__ == '__main__':
    db.connect()
    db.create_tables([Student], safe=True)
    add_students()
    print("Our top student right now is: {0.username}.".format(top_student()))

treehouse:~/workspace$ python students.py
Our top student right now is: jeremy.
treehouse:~/workspace$

treehouse:~/workspace$ python students.py
Our top student right now is: jeremy.
treehouse:~/workspace

1 Answer

Seth Kroger
Seth Kroger
56,414 Points

When the student record already exists and you are trying to update the points you're assigning to student_record_points which is creating a new variable, not updating the points property in the Student object. You should use student_record.points instead.

        except IntegrityError:
            student_record = Student.get(username = student['username'])
            student_record.points = student['points']
            student_record.save()
Jeremy Kerrigan
Jeremy Kerrigan
12,002 Points

Ohh my gosh. Thanks soo much Seth Kroger