Почему я получаю эту ошибку в tkinter
import tkinter as tk from tkinter import ttk import mysql.connector conn = mysql.connector.connect(host='localhost', username='root', password='admin') my_cursor = conn.cursor() my_cursor.execute("CREATE DATABASE IF NOT EXISTS EMPLOYEE") my_cursor.execute("USE EMPLOYEE") my_cursor.execute("CREATE TABLE IF NOT EXISTS EMPLOYEE_DATA(ID INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(255), SALARY INT)") def save_data(name, salary): query_insert = "INSERT INTO EMPLOYEE_DATA(NAME, SALARY) VALUES(%s, %s)" my_cursor.execute(query_insert, (name, salary)) conn.commit() def cal_sal(absent, leave, salarydb): total_days = 30 int(salarydb) perday = salarydb / 30 halfday = perday/2 absent_days = absent*perday leave_days = leave*perday total_sal = salarydb-(absent_days+leave_days) print(total_sal) # tkinter window------------------------------------- root = tk.Tk() root.title("Monthly Pay") root.geometry('440x550') root.resizable(0,0) def reg_new_employee(): newWindow = tk.Toplevel() newWindow.title("Register") newWindow.geometry('440x550') newWindow.resizable(0, 0) lb2 = tk.Label(newWindow, text='NEW ENTRY', font=('Calibri (Body)', 30)) lb2.pack() lbname = tk.Label(newWindow, text='NAME:', font=(10)) lbname.place(x=10 ,y=80) lbname_entry = tk.Entry(newWindow) lbname_entry.place(x=110, y=82) lbsalary = tk.Label(newWindow, text='SALARY:', font=(10)) lbsalary.place(x=10, y=120) lbsalary_entry = tk.Entry(newWindow) lbsalary_entry.place(x=110, y=122) btn_save = tk.Button(newWindow, text='SAVE', command= lambda: save_data(lbname_entry.get(), lbsalary_entry.get())) btn_save.place(x=180, y=200) def make_salary(): win = tk.Toplevel() win.title("Make Salary") win.geometry('440x550') win.resizable(0, 0) lb_title = tk.Label(win, text = 'Select Employee: ', font = 10) lb_title.place(x = 5, y = 1) # FETCH DATA TO SELECT EMPLOYEE COMBOBOX FROM DATABASE----------- query_fetchdata = "SELECT NAME FROM EMPLOYEE_DATA" my_cursor.execute(query_fetchdata) vl = tuple([name for name in my_cursor]) combobox_name = ttk.Combobox(win, width=15, state = 'readonly') combobox_name['values'] = vl combobox_name.current(0) combobox_name.place(x =150 , y = 1) lb_p = tk.Label(win, text = 'Present: ', font = 10) lb_p.place(x=5, y=200) lb_a = tk.Label(win, text = 'Absent: ', font = 10) lb_a.place(x=5, y=240) lb_h = tk.Label(win, text = 'Leave: ', font = 10) lb_h.place(x=5, y=280) lb_p_entry = tk.Entry(win) lb_p_entry.place(x= 100, y= 200) lb_a_entry = tk.Entry(win) lb_a_entry.place(x=100, y=240) lb_h_entry = tk.Entry(win) lb_h_entry.place(x=100, y=280) ##### I'm getting error here query_getname = "SELECT salary FROM EMPLOYEE_DATA WHERE NAME= '%s'" val = combobox_name.get() my_cursor.execute(query_getname, (val,)) btn_gen_sal = tk.Button(win, text = 'Generate Salary',) btn_gen_sal.place(x=180 ,y=450) lbl = tk.Label(root, text = 'MONTHLY PAY', font = ('Calibri (Body)', 35)) lbl.pack() btn_new_employee = tk.Button(root, text = 'New Employee', command = lambda: reg_new_employee()) btn_new_employee.place(x=180 ,y=200) btn_make_salary = tk.Button(root, text = 'Make Salary', command = lambda: make_salary()) btn_make_salary.place(x=180 ,y=290) root.mainloop()
ОШИБКА:
C:\Users\RISHI\AppData\Local\Programs\Python\Python37\python.exe C:/Users/RISHI/PycharmProjects/untitled/monthy_pay.py Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\RISHI\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection_cext.py", line 489, in cmd_query raw_as_string=raw_as_string) _mysql_connector.MySQLInterfaceError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'F''' at line 1 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\RISHI\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:/Users/RISHI/PycharmProjects/untitled/monthy_pay.py", line 116, in <lambda> btn_make_salary = tk.Button(root, text = 'Make Salary', command = lambda: make_salary()) File "C:/Users/RISHI/PycharmProjects/untitled/monthy_pay.py", line 102, in make_salary my_cursor.execute(query_getname, (val,)) File "C:\Users\RISHI\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\cursor_cext.py", line 266, in execute raw_as_string=self._raw_as_string) File "C:\Users\RISHI\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection_cext.py", line 492, in cmd_query sqlstate=exc.sqlstate) mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'F''' at line 1
Что я уже пробовал:
Пожалуйста, помогите мне, ребята, а также я новичок в python, поэтому, пожалуйста, объясните простым языком.
0x01AA
Попробуй my_cursor.execute(query_getname, val)
вместо my_cursor.execute(query_getname, (val,))
Richard MacCutchan
Это решение проблемы.
0x01AA
Поскольку я не знаю Python и заметил только странный синтаксис для значения параметра, я написал это только как комментарий. Но если вы уверены, что это ошибка, вы можете написать это как ответ.
Richard MacCutchan
Я не проверял, просто предположил, что ты знаешь.