import sqlite3
import re
from tkinter import messagebox
onlyNumber = r'^d+$'
onlyLetters = r'^[a-zA-Zs]+$'
onlyAge = r'^d+$'
def insertStudent(student_id, full_name, age):
if re.match(onlyNumber, student_id) and re.match(onlyLetters, full_name) and re.match(onlyAge, age):
try:
with sqlite3.connect('school.sqlite') as conn:
cursor = conn.cursor()
cursor.execute("INSERT INTO student (StudentID, FullName, Age) VALUES (?, ?, ?)",
(student_id, full_name, age))
conn.commit()
messagebox.showinfo("Success", "Student successfully added to the database.")
except sqlite3.Error as e:
print(f"An error occurred: {e}")
messagebox.showerror("Error", "An error occurred while adding the student to the database.")
else:
print("Invalid input. Only numbers allowed for ID and Age, and only letters for Full Name.")
messagebox.showerror("Invalid Input", "Please enter valid data.")
from tkinter import *
import customtkinter as ctk
from school.backendSchool import insertStudent
windowMenu = ctk.CTk()
studentEntry = ctk.CTk()
windowMenu.title("Menu")
windowMenu.geometry("1280x720")
def buttonToMenu():
studentEntry.withdraw()
windowMenu.deiconify()
def buttonToStudent():
windowMenu.withdraw()
studentEntry.title("Student Entry")
studentEntry.geometry("1280x720")
studentEntry.deiconify()
entryStudentID = ctk.CTkEntry(studentEntry)
entryStudentID.pack(anchor=CENTER)
entryStudentName = ctk.CTkEntry(studentEntry)
entryStudentName.pack(anchor=CENTER)
entryStudentAge = ctk.CTkEntry(studentEntry)
entryStudentAge.pack(anchor=CENTER)
buttonStudentTable = ctk.CTkButton(studentEntry, text="Create Student",
command=lambda: insertStudent(entryStudentID.get().strip(),
entryStudentName.get().strip(),
entryStudentAge.get().strip()))
buttonStudentTable.pack(anchor=CENTER)
buttonStudent = ctk.CTkButton(windowMenu, text="Student", command=buttonToStudent)
buttonStudent.pack(anchor=CENTER)
buttonMenu = ctk.CTkButton(studentEntry, text="Menu", command=buttonToMenu)
buttonMenu.pack(anchor=CENTER)
buttonToQuit = ctk.CTkButton(windowMenu, text="Quit", command=windowMenu.destroy)
buttonToQuit.pack(anchor=CENTER)
buttonToQuitStudent = ctk.CTkButton(studentEntry, text="Quit", command=studentEntry.destroy)
buttonToQuitStudent.pack(anchor=CENTER)
windowMenu.mainloop()
I have the problem that after typing some information into the entries and clicking on the insert data button i get the exception all the time, i was not able to find the cause of this exception.
I tried everything and even using AI!
Please help.
I added the picture for potential project structure questions
New contributor
Blend is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.