python - 从 SQL 语句中获取多个值

标签 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 个查询即可获得计数:

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

关于python - 从 SQL 语句中获取多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28024428/

相关文章:

python - 如何提取 float 的分子、分母和根?

python - Django 使用外键添加到数据库,同时仍显示来自其他模型的信息

mysql - 如何创建 MySQL 函数来输出值从 1 到给定数字的查询?

Mysql - 如何退出/退出存储过程

python - Flask、gunicorn、redis - 第 3 条路线达到 500,但 POST 在前面的步骤中有效

c - 追踪大型源代码

python - 倒花式索引

mysql - 设置要反转的 MySQL 表的默认模式顺序

具有多个相等标准的 Java Set

python - 遍历用户输入和列表