I have created small project about students registration. I am using Python and MySQL. I want to how find students registered with specific date. I have tried and created the following codes:
import tkinter as tk
from tkinter import ttk
from tkcalendar import DateEntry
import mysql.connector
my_w = tk.Tk()
my_w.geometry("900x650") # Size of the window
my_w.title("Tutorial") # Adding a title
sel=tk.StringVar()
cal=DateEntry(my_w,selectmode='day',textvariable=sel)
cal.grid(row=0,column=0,padx=20,pady=30)
def my_upd(*args): # triggered when value of string varaible changes
if(len(sel.get())>4):
dt=cal.get_date() # get selected date object from calendar
dt1=dt.strftime("%Y-%m-%d") #format for MySQL date column
dt2=dt.strftime("%d-%B-%Y") #format to display at label
l1.config(text=dt2) # display date at Label
con =mysql.connector.connect(host="localhost",database='tutorial',user="root",password="")
cur=con.cursor()
query="SELECT * from students WHERE regisdate=%s"
r_set=cur.execute(query,dt1) # execute query with data
for item in trv.get_children(): # delete all previous listings
trv.delete(item)
total=0 # to store total sale of the selected date
for dt in r_set:
trv.insert("", 'end',iid=dt[0], text=dt[0],
values =(dt[0],dt[1],dt[2],dt[3]))
total=round(total+(dt[2]*dt[3]),2)
l2.config(text="Total: " + str(total)) # show total value
l1=tk.Label(my_w,font=('Times',22,'bold'),fg='blue')
l1.grid(row=0,column=1)
trv = ttk.Treeview(my_w, selectmode ='browse')
trv.grid(row=1,column=1,padx=20,pady=20)
# number of columns
trv["columns"] = ("0", "1", "2","3")
trv['height'] =20
# Defining heading
trv['show'] = 'headings'
# width of columns and alignment
trv.column("0", width = 30, anchor ='c')
trv.column("1", width = 80, anchor ='c')
trv.column("2", width = 80, anchor ='c')
trv.column("3", width = 80, anchor ='c')
# Headings
# respective columns
trv.heading("0", text ="ID")
trv.heading("1", text ="Name")
trv.heading("2", text ="Contact")
trv.heading("3", text ="RegisDate")
sel.trace('w',my_upd)
l2=tk.Label(my_w,font=('Times',22,'bold'),fg='red')
l2.grid(row=1,column=2,sticky='ne',pady=20)
my_w.mainloop() # Keep the window open
When i run the above code, it raises this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Siciid\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "c:\Users\Siciid\Desktop\Tutorial\datewise.py", line 22, in my_upd
r_set=cur.execute(query,dt1) # execute query with data
File "C:\Users\Siciid\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\cursor_cext.py", line 257, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "C:\Users\Siciid\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 686, in prepare_for_mysql
raise errors.ProgrammingError(
mysql.connector.errors.ProgrammingError: Could not process parameters: str(2022-07-25), it must be of type list, tuple or dict
Please i need your help.