0

While I am trying to insert a row to my table from the Tkinter entries widgets (Entry, ComboBox, Radio Button), I'm getting the following errors:

ERROR 1136(21S01): Column count doesn't match value count at row 1.

Column names:

Course, 
U_Id, 
Subject, 
Years, 
Semester, 
Student_Names, 
Roll_No, 
Gender, 
DOB, 
Email, 
Mobile, 
Address, 
Photo

where U_Id is auto increment,

and values: crsVar.get(), sbVar.get(), yrsVar.get(), smVar.get(),nVar.get(), rollVar.get(), genVar.get(), dVar.get(), eVar.get(), mobVar.get(), adVar.get(), rdVar.get()

Please help me out, this is my code

try:
    conn = mysql.connector.connect(host="localhost", username="root", 
    password="Sahil#12", database="attendancesystem")

    c = conn.cursor()
    c.execute('insert into `students_detail` values(crsVar.get(), sbVar.get(),
                        yrsVar.get(), smVar.get(), nVar.get(), rollVar.get(), 
                        genVar.get(), dVar.get(), eVar.get(), mobVar.get(),
                         adVar.get(), rdVar.get()))                          

    conn.commit()
    conn.close()
    messagebox.showinfo("Success", "Students details has been submitted", 
                                  parent=self.master)

except Exception as e:
    messagebox.showerror("Error", f"Due to {str(e)}")
8349697
  • 415
  • 1
  • 6
  • 11
Sahil
  • 3
  • 5
  • 3
    You have 13 columns, but only 12 values. Obviously you do not want to specify U_ID since it's an auto-inc column, but that means you have to specify the columns explicitely. – Mike Scotty Jul 08 '21 at 14:03
  • first of all you are using `'` wrong – bangKok Jul 08 '21 at 14:06
  • Does this answer your question? [How to insert new row to database with AUTO\_INCREMENT column without specifying column names?](https://stackoverflow.com/questions/3493612/how-to-insert-new-row-to-database-with-auto-increment-column-without-specifying) – Delrius Euphoria Jul 08 '21 at 14:10
  • Use `'''` if you are splitting a single query into multi-lines – Delrius Euphoria Jul 08 '21 at 14:11
  • Mandatory [Bobby Tables](https://xkcd.com/327/) link. – Robert Jul 08 '21 at 18:00

2 Answers2

0

The issue here is that you have 12 values and 13 columns due to the auto-incremented U_Id. Your best option is to specify the columns of the table manually ie:

c.execute('insert into `students_detail` (Course, Subject, Years, ...)  values(crsVar.get(), sbVar.get(),
                    yrsVar.get(), ...))                          

(Note you do NOT include the U_Id field in either the columns or values).

Ther are alternatives: How to insert new row to database with AUTO_INCREMENT column without specifying column names? however specifying the column/value pairs is a more robust solution.

ILee
  • 31
  • 4
0

Try it this way : (use %s to avoid SQL injection)

c.execute("INSERT INTO students_detail "                                      
    "(value1, value2, value3, value4, value5)"           
    "VALUES (%s, %s, %s, %s, %s) ",                                
    (                                                              
        widget[0].get(),                     #
        widget[1].get(),                     #
        widget[2].get(),                     #
        widget[3].get(),                     #
        widget[4].get(),                     #
    )) ## this widget.get() is only an example, you will need to change all these values
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
bangKok
  • 332
  • 2
  • 13