'where clause'中未知列'name'



实际上,我在一个基于面部识别的考勤系统中工作。但不幸的是,我在Mysql记录考勤时遇到了问题

if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]  
sql = "UPDATE stud_att SET P1=1 WHERE name = fname"
mycursor.execute(sql)
mydb.commit()

draw.rectangle(((left, top), (right, bottom)), outline=(255,255,0))

text_width, text_height = draw.textsize(name)
draw.rectangle(((left,bottom - text_height - 10), (right, bottom)), fill=(255,255,0), outline=(255,255,0))
draw.text((left + 6, bottom - text_height - 5), name, fill=(0,0,0))

del draw

这是我的代码问题是Unknown column 'name' in 'where clause'我不知道哪里错了。我确信系统识别的名称在数据库中存在!

我的桌子

fname, lname, ID, P1, P2, P3, P4
Name1,  Name2, 1,
sql = "UPDATE stud_att SET P1=1 WHERE fname = " + name 

列名必须位于=的左侧,然后将其与要检查的值进行比较。

您的直接问题是引用问题:在SQL中,字符串需要用单引号括起来;假设name是一个字符串,则需要:

mycursor.execute("UPDATE stud_att SET P1 = 1 WHERE fname = '" + name + "'")

但这很容易出错,并且仍然将代码留给SQL注入。所以:您应该只使用参数化查询:

mycursor.execute("UPDATE stud_att SET P1 = 1 WHERE fname = %s", (name))

相关内容

最新更新