Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
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()


ERROR:

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


What I have tried:

Please help me guys and also I'm new to python so please explain in simple language.
Posted
Updated 16-May-20 2:31am
Comments
[no name] 16-May-20 8:29am    
Try
my_cursor.execute(query_getname, val)
instead of
my_cursor.execute(query_getname, (val,))
Richard MacCutchan 16-May-20 9:07am    
That's a solution.
[no name] 16-May-20 9:13am    
Because I do not know Python and I only noticed the strange syntax for the parameter value, I only wrote this as a comment. But if you are sure that this is the mistake, you are welcome to write this as an answer.
Richard MacCutchan 16-May-20 10:34am    
I didn't check it, just assumed you knew.

1 solution

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

Somewhere - probably in line 116 of your Python code, but I have no idea which one that is - your SQL syntax is wrong. So use the debugger to narrow down what line that is, and to look at what you are actually sending. We can't do that for you!

This may help: pdb — The Python Debugger — Python 3.8.3 documentation[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900