Search code examples
pythonsqldeduplication

getting multiple values out of an SQL statement


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")

Solution

  • Just use only 1 query where you get the count:

    cursor.execute("select Count(TeacherID) from TeacherInfo where TeacherInitials = ?",(TeacherInitials,))