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

James N
James N
17,864 Points

not getting error when supposed to LOL

on the video: "Queries are your friend" for the Python course: "Using databases in python", i'm not getting an error when i'm supposed to! the teacher said that the first time the program is ran, at 03:59, it works fine, followed by, the second time, errors popping up. no errors are popping up for me! here's my code:

students.py
from peewee import *

db = SqliteDatabase("students.db")


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

    class Meta:
        databse = db

students = [{"username": "kennethlove",
            "points": 4888},
           {"username": "jamesn",
            "points": 12999},
           {"username": "bobthezombie",
            "points": 0}]

def add_students():
  for student in students:
    Student.create(username=student["username"],
                  points=student["points"])

if __name__ == "__main__":
    db.connect()
    db.create_tables([Student], safe=True)
    db.close()

and my output:

output.txt
treehouse:~/workspace$ python students.py                                                                                                                                                                        
treehouse:~/workspace$ python students.py                                                                                                                                                                        
treehouse:~/workspace$ python students.py                                                                                                                                                                        
treehouse:~/workspace$ python students.py                                                                                                                                                                        
treehouse:~/workspace$

as you can see, i ran this a few times. thanks for your help!

1 Answer

James N
James N
17,864 Points

oh! i think i fixed it! all i did, was forget to call add_students() but i fixed it! i fixed it later on in the video so i didn't get any errors on uniqueness.

here's my UPDATED code:

students_updated.py
from peewee import *

db = SqliteDatabase("students.db")


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

    class Meta:
        databse = db

students = [{"username": "kennethlove",
            "points": 4888},
           {"username": "jamesn",
            "points": 12999},
           {"username": "bobthezombie",
            "points": 0}]

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

if __name__ == "__main__":
    db.connect()
    db.create_tables([Student], safe=True)
    add_students()   #fixed!
    db.close()