【问题标题】:I'm having trouble with an SQL syntax error [closed]我遇到了 SQL 语法错误的问题 [关闭]
【发布时间】:2020-10-19 08:55:54
【问题描述】:

我正在尝试将数据添加到在 tkinter GUI 上收集的表中。这个错误不断出现:

Tkinter 回调中的异常 回溯(最近一次通话最后): 文件“C:\Users\xande\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection_cext.py”,第 489 行,在 cmd_query raw_as_string=raw_as_string) _mysql_connector.MySQLInterfaceError:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'Rank,Email,Phone) VALUES(5509,'mandy123','password123','Mike','Andy','MJR','mand ' 在第 1 行 在处理上述异常的过程中,又出现了一个异常: 回溯(最近一次通话最后): 文件“C:\Users\xande\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py”,第 1705 行,在 __call__ 返回 self.func(*args) 文件“C:\Users\xande\OneDrive\Desktop\School\YR 14\CCF program\CCF code.py”,第 146 行,在 addldrsubmit mycursor.execute(insertSQL,(LID,str(lunentry),str(lpwentry),str(lfnentry),str(lsnentry),str(lrankentry),str(lemailfullentry),lphoneentry,)) 执行中的文件“C:\Users\xande\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\cursor_cext.py”,第 266 行 raw_as_string=self._raw_as_string) 文件“C:\Users\xande\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection_cext.py”,第 492 行,在 cmd_query sqlstate=exc.sqlstate) mysql.connector.errors.ProgrammingError: 1064 (42000): 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 'Rank,Email,Phone) VALUES(5509,'mandy123','password123','Mike','Andy','MJR','mand ' 在第 1 行

我使用的代码是:

import mysql.connector
if True:
    mydb = mysql.connector.connect(
        host="localhost",
        user="root",
        passwd="abarclay172",
        database="rbaiccf"
        )
mycursor=mydb.cursor()

LID = random.randint(1000,9999)
    lunentry = lunentrye.get()
    lpwentry = lpwentrye.get()
    lfnentry = lfnentrye.get()
    lsnentry = lsnentrye.get()
    lrankentry = addldrranke.get()
    lemailentry = lemailentrye.get()
    lemailaddressentry = addldremailaddresse.get()
    lemailfullentry = lemailentry + lemailaddressentry
    lphoneentry = "+44" + lphoneentrye.get()
insertSQL="""INSERT INTO leader(leaderID,Username,Password,Forename,Surname,Rank,Email,Phone) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)"""
    mycursor.execute(insertSQL,(LID,str(lunentry),str(lpwentry),str(lfnentry),str(lsnentry),str(lrankentry),str(lemailfullentry),lphoneentry,))
    mydb.commit()

【问题讨论】:

  • 你需要共享表leader定义。
  • 为什么在将insertSQL 传递给execute 之前不打印它?这样你就可以看到它在做什么?
  • 请修正代码的缩进。使用反引号 (`) 排名作为列名,它可能会解决问题。

标签: python mysql python-3.x tkinter


【解决方案1】:

根据您提供的描述,我猜是使用 RANK 作为列名。这是一个reserved keyword。你可以试试this solution - 使用反引号。

但是如果还有其他事情发生,您应该提供更多信息,如 cmets 中所述。表定义,完整的 SQL,你正在尝试执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    相关资源
    最近更新 更多