从SQL语句中获取多个值

时间:2015-01-19 12:13:31

标签: python sql deduplication

我想要弄清楚是否有老师有重复的姓名缩写。我试图通过从搜索到的首字母的数据库文件中返回一个值来完成此操作。然后返回搜索到的首字母的所有值。然后我想检查第一个值与第二个值,如果它们不相等则必须有重复。

有没有办法做到这一点并且有更简单的方法吗?

由于

 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 个答案:

答案 0 :(得分:1)

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

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