从 SQL 语句中获取多个值

getting multiple values out of an SQL statement

我想知道是否有老师的姓名首字母重复。我试图通过从数据库文件中返回一个带有搜索到的首字母的值来做到这一点。然后返回所有带有搜索到的首字母的值。然后我想检查第一个值与第二个值,如果它们不相等,则必须重复。

有没有办法做到这一点,有没有更简单的方法?

谢谢

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

只需使用 1 个查询即可获得计数:

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