So, I am creating a simple GUI in Python
using Tkinter
.
Now, when the user leaves the Roll Number
(i.e. primary key) label widget empty and tries to insert the data, I will be inserting None
instead of an empty string and I want to throw an Error
but instead the data gets inserted with an auto-incremented Roll Number value. I don't want the Primary Key to get inserted if the Roll Number label widget is left empty.
This the code I am using for Creating the Table
c.execute("""
CREATE TABLE students(
enroll_no integer NOT NULL PRIMARY KEY,
first_name text NOT NULL,
middle_name text,
last_name text,
course text NOT NULL,
semester integer NOT NULL
)
""")
And this the code for Inserting into the Table
try:
c.execute("INSERT INTO students VALUES (:inp_rn, :inp_fn, :inp_mn, :inp_ln, :inp_course, :inp_semester)",
{
'inp_rn':inp_rn.get() if inp_rn.get() != "" else None,
'inp_fn':inp_fn.get() if inp_fn.get() != "" else None,
'inp_mn':inp_mn.get() if inp_mn.get() != "" else None,
'inp_ln':inp_ln.get() if inp_ln.get() != "" else None,
'inp_course':inp_course.get() if inp_course.get() != "" else None,
'inp_semester':inp_semester.get() if inp_semester.get() != "" else None
}
)
except sqlite3.IntegrityError:
messagebox.showerror("Insertion Failed","Insertion Failed")