在 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

解决方法

sql_update_query = """ UPDATE EmployeeList SET %s = %s WHERE %s = %s;"""

最后添加一个分号(;)并检查一次是否有效。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...