skip to Main Content

My code:

  def update(self):
            conn=mysql.connector.connect(host="localhost",user="root",password="root",database="pathology")
            my_cursor=conn.cursor()       
            my_cursor.execute("update pathology set num=%s,receivedate=%s,patientname=%s,pathologynum=%s,gender=%s,age=%s,inspechospital=%s,casenum=%s,specimenclass=%s,bed=%s,diagnosis=%s,clinicaldiagnose=%s,inspecdoctor=%s,pathologydoctor=%s,sampledate=%s,reportdate=%s",(
                                                                                                self.var_sys_num.get(),
                                                                                                self.var_rec_date.get(),
                                                                                                self.var_patient_name.get(),
                                                                                                self.var_path_num.get(),
                                                                                                self.var_gender.get(),
                                                                                                self.var_age.get(),
                                                                                                self.var_sub_hospital.get(),
                                                                                                self.var_case_num.get(),
                                                                                                self.var_specimen_level.get(),
                                                                                                self.var_bed.get(),
                                                                                                self.var_diagnosis.get(),
                                                                                                str(contents),                                                                         
                                                                                                self.var_submit_doctor.get(),
                                                                                                self.var_simple_date.get(),
                                                                                                self.var_path_doc.get(),
                                                                                                self.var_report_date.get()             
            ))

I want "pathology" table in MySQL Workbench to be updated when clicking on the "update" button.

but the error: mysql.connector.errors.IntegrityError: 1062 (23000): Duplicate entry ‘430804’ for key ‘pathology.PRIMARY’

2

Answers


  1. Your statement seems to be incomplete, it should contain ‘where’ clause to identify which record you want to update. Without it mysql may try to insert given values to all the rows of the table, which can lead to a primary key conflict.

    Login or Signup to reply.
  2. Try this and update id information which is mentioned in comment:

    def update(self):
        conn = mysql.connector.connect(host='localhost', user='root',
                                       password='root', database='pathology'
                                       )
        my_cursor = conn.cursor()
        my_cursor = conn.cursor(prepared=True)
    
        sql_update_query = """update pathology set num=%s, receivedate=%s, patientname=%s, pathologynum=%s, gender=%s, age=%s, inspechospital=%s, casenum=%s, specimenclass=%s, bed=%s, diagnosis=%s, clinicaldiagnose=%s, inspecdoctor=%s, pathologydoctor=%s, sampledate=%s, reportdate=%s where id = %s"""
    
        data_tuple = (
            self.var_sys_num.get(),
            self.var_rec_date.get(),
            self.var_patient_name.get(),
            self.var_path_num.get(),
            self.var_gender.get(),
            self.var_age.get(),
            self.var_sub_hospital.get(),
            self.var_case_num.get(),
            self.var_specimen_level.get(),
            self.var_bed.get(),
            self.var_diagnosis.get(),
            str(contents),
            self.var_submit_doctor.get(),
            self.var_simple_date.get(),
            self.var_path_doc.get(),
            self.var_report_date.get(),
            # your id/column info against which you want to update
        )
    
        my_cursor.execute(sql_update_query, data_tuple)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search