mysql和python错误:当我试图从tkinter小部件(条目、组合框、单选按钮)向表中插入一行时,我遇到了一个错误

ffdz8vbo  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(314)

当我试图从tkinter entries小部件(条目、组合框、单选按钮)向表中插入一行时,出现以下错误: ERROR 1136(21S01): Column count doesn't match value count at row 1. 列名:

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

其中u_id为自动增量,
和值:crsvar.get(),sbvar.get(),yrsvar.get(),smvar.get(),nvar.get(),rollvar.get(),genvar.get(),dvar.get(),evar.get(),mobvar.get(),advar.get(),rdvar.get()
请帮帮我,这是我的密码

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)}")
cnjp1d6j

cnjp1d6j1#

这里的问题是,由于自动递增的u_id,您有12个值和13列。您最好的选择是手动指定表的列,即:

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

(请注意,列或值中均不包含u_id字段)。
还有其他选择:如何在不指定列名的情况下使用自动增量列向数据库插入新行?但是,指定列/值对是一个更健壮的解决方案。

bq3bfh9z

bq3bfh9z2#

这样试试:(使用 %s 避免sql注入)

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

相关问题