python tkinter 你的 SQL 语法有错误

问题描述

有人可以帮忙吗?

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\huzey\AppData\Local\Programs\Python\python39\lib\site-packages\MysqL\connector\connection_cext.py",line 506,in cmd_query
_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 ')' at line 1

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\Users\huzey\AppData\Local\Programs\Python\python39\lib\tkinter\__init__.py",line 1892,in __call__
    return self.func(*args)
  File "c:\Users\huzey\OneDrive\Masaüstü\gui\loginsystem.py",line 198,in add_customer
    my_cursor.execute(sql_command,values)
  File "C:\Users\huzey\AppData\Local\Programs\Python\python39\lib\site-packages\MysqL\connector\cursor_cext.py",line 269,in execute
    result = self._cnx.cmd_query(stmt,raw=self._raw,File "C:\Users\huzey\AppData\Local\Programs\Python\python39\lib\site-packages\MysqL\connector\connection_cext.py",line 510,in cmd_query
    raise errors.get_MysqL_exception(exc.errno,msg=exc.msg,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 ')' at line 1

这是完整的错误,这是有错误代码部分:

def add_customer():
    sql_command = "INSERT INTO customers (first_name,last_name,addres,phone,email,age,country,rent) VALUES (%s,%s,)"
    values = (first_name_Box.get(),last_name_Box.get(),address_Box.get(),phone_Box.get(),email_Box.get(),age_Box.get(),country_Box.get(),rent_Box.get())
    my_cursor.execute(sql_command,values)
    mydb.commit()
    clear_all()

顺便说一句,我正在尝试制作一个房地产管理系统。如果您有任何想法,我可以接受新想法和代码,如果您愿意,我可以提供详细信息。

解决方法

最后多了一个逗号:

... VALUES (%s,%s,)

摆脱它——当你在做的时候,你可以将语句分成多个文字以提高可读性:

sql_command = (
    "INSERT INTO customers "
    "(first_name,last_name,addres,phone,email,age,country,rent) "
    "VALUES (%s,%s)"
)