微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?