如何解决在 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;"""
最后添加一个分号(;
)并检查一次是否有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。