【发布时间】:2021-08-14 10:29:23
【问题描述】:
mysql.connector.errors.ProgrammingError: 1064 (42000): 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“%s”附近使用正确的语法
代码:
def delete_data(self):
con = mysql.connector.connect(host="localhost", user="root", password="Satpute@223", database="stm")
cur = con.cursor()
cur.execute("DELETE FROM students WHERE roll_no=%s",self.Roll_No_var.get())
con.commit()
con.close()
self.fetch_data()
self.clear()
def search_data(self):
con = mysql.connector.connect(host="localhost", user="root", password="Satpute@223", database="stm")
cur = con.cursor()
cur.execute("select * from students where"+str(self.search_by.get())+" like '%"+str(self.search_text.get())+"%'")
rows = cur.fetchall()
if len(rows) != 0:
self.Student_table.delete(*self.Student_table.get_children())
for row in rows:
self.Student_table.insert('', END, values=row)
con.commit()
con.close()
【问题讨论】:
-
search_data()里面的SQL里面的where后面应该有一个空格。 -
永远不要使用
cur.execute("string_expression")- 总是使用sql_text = "string_expression"然后cur.execute(sql_text)。这允许打印sql_text变量值并检查 SQL 是否有错误。 -
请不要使用字符串格式或连接来准备查询。这些很容易受到注入攻击。请改用prepared statements。
标签: python mysql tkinter mysql-python mysql-error-1064