首页 > 解决方案 > MySQL更新集合中的多个变量

问题描述

好吧,我有一个字典作为输入,例如:{'date':2020}。

关键是表的列名,我只是想将值更新到表中

string = ["{} = '{}'".format(k,v) for k,v in parameter_dict.items()]
string_joined =", ".join(string)
query = "UPDATE experiment SET" + string_joined + ' ' + 'WHERE ID =' + str(experimentID)
cursor.execute(query)

上面的代码导致错误

_mysql.connector.errors.ProgrammingError: 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 '= 'Mar-2020-16_25_18', training_minutes = '0' WHERE ID =21' at line 1


"UPDATE experiment SET %s WHERE ID =%s", (string_joined, experimentID)

使用上面的语句也会导致同样的错误

mysql.connector.errors.ProgrammingError: 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 ''date_end = \'Mar-2020-16_18_52\', training_minutes = \'0\'' WHERE ID = 20' at line 1

还尝试将单个更新放入 for 循环,但这会导致另一个错误!

那么我该如何解决呢?它真的不能将变量发送到 SET 分区吗?谢谢

标签: pythonmysql

解决方案


推荐阅读