我想要找出是否有老师姓名首字母重复。我尝试通过从数据库文件中返回一个值以及搜索到的缩写来做到这一点。然后返回带有搜索到的首字母缩写的所有值。然后我想检查第一个值与第二个值,如果它们不相等,则必须有重复项。
有没有办法做到这一点,有没有更简单的方法?
谢谢
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/