MySQL and Python Error : While I am trying to insert a row to my table from the Tkinter widget's(Entry, ComboBox, Radio Button), I'm getting an error
MySQL and Python Error : While I am trying to insert a row to my table from the Tkinter widget's(Entry, ComboBox, Radio Button), I'm getting an error
当我尝试从 Tkinter 条目小部件(条目、组合框、单选按钮)向我的 table 插入一行时,出现以下错误:
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)}")
这里的问题是由于自动递增 U_Id,您有 12 个值和 13 个列。您最好的选择是手动指定 table 的列,即:
c.execute('insert into `students_detail` (Course, Subject, Years, ...) values(crsVar.get(), sbVar.get(),
yrsVar.get(), ...))
(请注意,您不在列或值中包含 U_Id 字段)。
还有其他选择:How to insert new row to database with AUTO_INCREMENT column without specifying column names? 但是指定 column/value 对是更可靠的解决方案。
这样试试:
(使用%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
当我尝试从 Tkinter 条目小部件(条目、组合框、单选按钮)向我的 table 插入一行时,出现以下错误:
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)}")
这里的问题是由于自动递增 U_Id,您有 12 个值和 13 个列。您最好的选择是手动指定 table 的列,即:
c.execute('insert into `students_detail` (Course, Subject, Years, ...) values(crsVar.get(), sbVar.get(),
yrsVar.get(), ...))
(请注意,您不在列或值中包含 U_Id 字段)。
还有其他选择:How to insert new row to database with AUTO_INCREMENT column without specifying column names? 但是指定 column/value 对是更可靠的解决方案。
这样试试:
(使用%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