How to solve "Due To:Not all parameters were used in the SQL statement" Error? I am getting this error in below code

53 views Asked by At
#""""""""""""" UPDATE FUNCTION """"""""""
    def update_data(self):
      if self.var_dep.get()=="Select Department" or self.var_std_name.get()=="" or self.var_std_id.get()=="":
         messagebox.showerror("Error","All Fields are required",parent=self.root)
      else:
          try:
              update=messagebox.askyesno("Update","Do you want to update this student details",parent=self.root)
              if update>0:
                conn=mysql.connector.connect(host="localhost", username="root", password="Umer@123", database="face_recognizer")
                my_cursor=conn.cursor()
                my_cursor.execute("update student set Dep=%s,course=%s,Year=%s,Semester=%s,Division=%s,Roll=%s,Gender=%s,Dob=%s,Email=%s,Phone=%s,Address=%s,Teacher=%s,PhotoSample=%s where Student_id=%s",(

                                                                                                                                                                          self.var_dep.get(),
                                                                                                                                                                          self.var_course.get(),
                                                                                                                                                                          self.var_year.get(),
                                                                                                                                                                          self.var_semester.get(),
                                                                                                                                                                          self.var_std_name.get(),
                                                                                                                                                                          self.var_div.get(),
                                                                                                                                                                          self.var_roll.get(),
                                                                                                                                                                          self.var_gender.get(),
                                                                                                                                                                          self.var_dob.get(),
                                                                                                                                                                          self.var_email.get(),
                                                                                                                                                                          self.var_phone.get(),
                                                                                                                                                                          self.var_address.get(),
                                                                                                                                                                          self.var_teacher.get(),
                                                                                                                                                                          self.var_radio1.get(),
                                                                                                                                                                          self.var_std_id.get()
                                                                                                                                                                           ))
              else:
                if not update:
                    return
              conn.commit()
              self.fetch_data()
              conn.close()

              messagebox.showinfo("Sucess","Student details successfully update completed",parent=self.root)
          except Exception as es:
              messagebox.showerror("Error",f"Due To:{str(es)}",parent=self.root)

I am not able to find the error.

1

There are 1 answers

0
1966bc On

Excuse me, sir, but your code is really horrible....

Maybe decoupling a little bit

    args = (self.var_dep.get(),
        self.var_course.get(),
        self.var_year.get(),
        self.var_semester.get(),
        self.var_std_name.get(),
        self.var_div.get(),
        self.var_roll.get(),
        self.var_gender.get(),
        self.var_dob.get(),
        self.var_email.get(),
        self.var_phone.get(),
        self.var_address.get(),
        self.var_teacher.get(),
        self.var_radio1.get(),
        self.var_std_id.get())

sql = "UPDATE student SET Dep=?,course=?,Year=?,Semester=?,Division=?,Roll=?,Gender=?,Dob=?,Email=?,Phone=?,Address=?,Teacher=?,PhotoSample=?\
       WHERE Student_id=?"

my_cursor.execute(sql, args)

and you can see that the number of variables in args is different from the number expected by the query.

But anyway these things are not done that way, Ifyou one day decide to increase or decrease the number of fields what do you do?