1

What I am trying to is work out whether there are teachers with duplicate initials. I tried to do this by returning one value from the database file with the searched for initials. Then returning all the values with the searched initials. Then I wanted to check the first value against the second, and if they were not equal there must be a duplicate.

Is there a way of doing this and is there an easier way of doing this?

Thanks

 def FindTeacherID(TeacherInitials):
        with sqlite3.connect("TeacherInfo.db") as db:
                cursor = db.cursor()
                cursor.execute("select TeacherID from TeacherInfo where TeacherInitials = ?",(TeacherInitials,))
                Value = cursor.fetchone()
                cursor.execute("select TeacherID from TeacherInfo where TeacherInitials =?",(TeacherInitials,))
                ValueTest = cursor.fetchall()
                if Value == None:
                    print("There are no teachers in the list")
                else:
                    Value = str('.'.join(str(x) for x in Value))
                    ValueTest = str('.'.join(str(x) for x in Value))
                    if ValueTest == Value:
                        DeleteTeacher(Value)
                    else:
                        print("There is a duplicate in teacher initials")
Ben
  • 529
  • 1
  • 4
  • 8
  • What is `DeleteTeacher` supposed to do? Finding duplicate initials is easy, but it depends what you're then trying to do with it... – Jon Clements Jan 19 '15 at 12:19
  • `DeleteTeacher` is trying to find duplicate initials so that the user can pick which teacher they wish to delete. – Ben Jan 19 '15 at 12:23

1 Answers1

1

Just use only 1 query where you get the count:

cursor.execute("select Count(TeacherID) from TeacherInfo where TeacherInitials = ?",(TeacherInitials,))
apomene
  • 14,282
  • 9
  • 46
  • 72