在 MYSql 中使用 Python 更新数据时,出现错误

问题描述

我知道有一些语法错误,但无法找出来。我确保它被插入到正确的表中。

column_update = input("Enter the column name where Data has to be updated: ")
update_data = input("Enter the new value: ")
column_name = input("Enter the column to get the row: ")
row_value = input("Enter the row value: ")

try:
    sql_update_query = """ UPDATE EmployeeList SET %s = %s WHERE %s = %s """
    my_cursor.execute(sql_update_query,params = (column_update,update_data,column_name,row_value))
    mydb.commit()
    print("Record Updated Successfully")

except mysql.connector.Error as error:
    print("Failed to update record to database: {}".format(error))

finally:
    if (mydb.is_connected()):
        my_cursor.close()
        mydb.close()
        print("MySQL connection is closed")

我的桌子:

my_cursor.execute("CREATE TABLE IF NOT EXISTS EmployeeList(user_id INT AUTO_INCREMENT PRIMARY KEY,EMPID INT,Emp_Name VARCHAR(100),Designation VARCHAR(100),Role VARCHAR(100),Updated_by VARCHAR(100),LastUpdate TIMESTAMP DEFAULT NOW())")

必须在运行脚本后提供用户输入的详细信息。

Enter the column name where Data has to be updated: Role
Enter the new value: Software
Enter the column name to get the row: EMPID
Enter the column value: 1

错误:

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 ''Role' = 'Software' WHERE 'EMPID' = '1'' at line 1

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)