A python Program I wrote today
(Edited)
I wrote this python program to accept inputs (student ID, name and address) and then save it in a database. In addition, various kinds of exceptions are handled in the process.
import sqlite3
import tkinter
# Setting up the database and creating a table
database_connection = sqlite3.connect("students.db")
database_cursor = database_connection.cursor()
try:
database_cursor.execute("""CREATE TABLE student_info (STUDENT_ID VARCHAR(11) NOT NULL,
NAME VARCHAR(50) NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
PRIMARY KEY(STUDENT_ID))""")
except:
""
# Creating the interface
root = tkinter.Tk()
# Setting the width and height of the interface
root.geometry("640x300")
# Setting the title of the interface
root.title("My Application")
# creating the id label and positioning it on the interface
id_label = tkinter.Label(text="Student ID",font="Arial 10 bold")
id_label.grid(row=0,column=0,padx=20,pady=20)
# creating the id box and positioning it on the interface
id_box = tkinter.Entry(font="Arial 10 normal")
id_box.grid(row=0,column=1)
# creating the name label and positioning it on the interface
name_label = tkinter.Label(text="Name",font="Arial 10 bold")
name_label.grid(row=1,column=0)
# creating the name box and positioning it on the interface
name_box = tkinter.Entry(font="Arial 10 normal")
name_box.grid(row=1,column=1)
# creating the address label and positioning it on the interface
address_label = tkinter.Label(text="Digital Address",font="Arial 10 bold")
address_label.grid(row=2,column=0,pady=20)
# creating the address box and positioning it on the interface
address_box = tkinter.Entry(font="Arial 10 normal")
address_box.grid(row=2,column=1)
def clear():
error_label.config(text="")
# creating a function for Saving the info in the database
def save():
student_id = id_box.get()
name = name_box.get()
address = address_box.get()
if student_id:
if name:
if address:
query = database_cursor.execute("""SELECT * FROM student_info WHERE
STUDENT_ID=?""",(student_id,))
data = query.fetchall()
if not data:
print("no data")
if len(student_id)<=11:
if len(name)<=50:
if len(address)<=20:
print("in")
try:
database_cursor.execute("""INSERT INTO student_info
(STUDENT_ID, NAME, ADDRESS)
VALUES(?, ?, ?)""",(student_id, name, address))
database_connection.commit()
database_connection.close()
id_box.delete(0,len(student_id))
name_box.delete(0,len(name))
address_box.delete(0,len(address))
error_label.config(text="Saved
successfully",foreground="lime",font="Arial 12 bold")
error_label.after(3000, clear)
except:
error_label.config(text = "Something wrong
happened!",foreground="red",font="Arial 12 bold")
error_label.after(3000,clear)
else:
error_label.config(text = "The address is too
long!",foreground="red",font="Arial 12 bold")
error_label.after(3000,clear)
else:
error_label.config(text = "The name is too
long!",foreground="red",font="Arial 12 bold")
error_label.after(3000,clear)
else:
error_label.config(text = "The student ID is too
long!",foreground="red",font="Arial 12 bold")
error_label.after(3000,clear)
else:
error_label.config(text = "Someone is already using this
ID!",foreground="red",font="Arial 12 bold")
error_label.after(3000,clear)
else:
error_label.config(text = "The address can not be empty, please provide a
value for it!",foreground="red",font="Arial 12 bold")
error_label.after(3000,clear)
else:
error_label.config(text = "The name can not be empty, please provide a value for
it!",foreground="red",font="Arial 12 bold")
error_label.after(3000,clear)
else:
error_label.config(text="The student ID can not be empty, please provide a value for
it!",foreground="red",font="Arial 12 bold")
error_label.after(3000,clear)
# Creating a save button for saving the record and positioning it on the interface
button = tkinter.Button(text="SAVE",width=10,font="Arial 10 bold",command=save)
button.grid(row=3,column=1)
# error label
error_label = tkinter.Label(text="", font="Arial 10 bold",foreground="red")
error_label.place(x=50,y=200)
root.mainloop()
1
0
0.000
0 comments