【问题标题】:mysql.connector.errors.IntegrityError: 1062 (23000): Duplicate entry '3' for key 'PRIMARY'mysql.connector.errors.IntegrityError: 1062 (23000): 键 'PRIMARY' 的重复条目 '3'
【发布时间】:2021-09-04 04:51:22
【问题描述】:

我的代码有问题

我的表及其主键上有一个 id 并设置为自动增量

def register():

    if (t1.get()==""  or t2.get()=="" or t3.get()==""):
        messagebox.showinfo("Result","Please Complete the Provided Details!")

    else:
        databases = mysql.connector.connect(
        host ="localhost",
        user = "userdata",
        password = "",
        database = "facerecog"
        )

        cursors = databases.cursor()
        cursors.execute("SELECT * from record")
        result = cursors.fetchall()

这是我的 ID 设置

id = 2 #This is the ID That I set 

对于结果中的 x:

  id ++1 #auto increment from my python
    sql = "INSERT INTO record(ids,names,course_year,positions) values(%s, %s ,%s , %s)"
    val = (id, t1.get(), t2.get(), t3.get())
    cursors.execute(sql,val)
    databases.commit()

我得到的错误

mysql.connector.errors.IntegrityError: 1062 (23000): Duplicate entry '3' for key 'PRIMARY'

如何自动递增?

【问题讨论】:

    标签: python localhost mysql-connector


    【解决方案1】:

    如果您的 ID 在您的数据库表中自动递增,我认为您不应该在插入语句中包含 ID 列。当您插入值时,ID 列将自动填充到您的数据库表中。它不需要您在 sql 语句的列中输入任何值。不如试试:

      sql = "INSERT INTO record(names,course_year,positions) values(%s ,%s , %s)"
        val = ( t1.get(), t2.get(), t3.get())
    

    但我认为 Python 使用

        (t1.text(), t2.text(), t3.text())
    

    要将值设置到数据库中,并且您的 t1、t2、t3 必须是可以输入值的文本字段 (lineEdit),这一切都取决于您使用的内容

    【讨论】:

    • 是的,它工作了,谢谢,但我现在面临的问题是我的程序会将数据记录到我的数据库中,它会拍照4张照片。但是在数据库中有 4 条记录将记录相同的名称,但不会记录在 ID 上
    • 如果一个建议有效,我们希望你支持它。 (微笑)。关于你最近一期关于4图4记录的问题,你能解释一下吗?
    • 我的意思是我的程序如果你把文本放到 t1,t2,t3 并点击注册它需要 4 张照片。但是在我的数据库上它记录了 4 个数据示例 id 1,2,3,4 相同的文本
    • 您输入的是照片吗?我不明白照片的来源。
    • 回答我的代码 我的文件夹中的照片和发送到我的数据库的数据
    【解决方案2】:
    def register():
    
        if (t1.get()==""  or t2.get()=="" or t3.get()==""):
            messagebox.showinfo("Result","Please Complete the Provided Details!")
    
        else:
            databases = mysql.connector.connect(
            host ="localhost",
            user = "userdata",
            password = "",
            database = "facerecog"
            )
    
            cursors = databases.cursor()
            cursors.execute("SELECT * from record")
            result = cursors.fetchall()
    
            
    
        for x in result:
            
            sql = "INSERT INTO record(names,course_year,positions) values(%s ,%s , %s)"
            val = ( t1.get(), t2.get(), t3.get())
            cursors.execute(sql,val)
            databases.commit()
    
            face_classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
            def face_cropped(img):
                gray = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
                faces = face_classifier.detectMultiScale(gray, 1.3, 5)
                # scaling factor = 1.3
                # minimum neighbor = 5
                if faces is ():
                    return None
                for (x,y,w,h) in faces:
                    cropped_face = cv2.rectangle(img,(1, 0),(0,0),(255,0,0),1)
                return cropped_face
    
            cap = cv2.VideoCapture(0)
    
            img_id = 0
    

        while True:
            ret, frame = cap.read()
            if face_cropped(frame) is not None:
    
                img_id+=1
    
                face = cv2.resize(face_cropped(frame), (1024,768))
                #face = cv2.cvtColor(face,cv2.COLOR_BGR2RGB)
    
                file_name_path = "img/"+str (t1.get())+""+str(id)+"."+str(img_id)+".jpg"
    
                cv2.imwrite(file_name_path, face)
    
                cv2.putText(face, str(img_id), (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
    
                cv2.imshow("Cropped face", face)
    
            if cv2.waitKey(1)==1 or int(img_id)==4:
                break
    
        cap.release()
        cv2.destroyAllWindows()
    

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 2013-09-30
      • 1970-01-01
      相关资源
      最近更新 更多