python - 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 分区吗?谢谢
解决方案
推荐阅读
- selenium - 如何在 Chrome 浏览器的 Katalon 中处理或抑制警告“这种类型的文件可能会损害您的计算机。您要保留它吗?”
- django - django_celery_beat - “没有这样的表:main.django_celery_beat_solarschedule__old”同时更新“django_celery_beat_periodictask”
- c++ - 如何减少对大量纹理的绘图调用次数?
- python - 如何摆脱由 os.path.join 创建的转义字符?
- c# - 如何在招摇中排除请求有效负载中的属性
- python - 使用 shell scrapit 启动 pythonenvironment 并执行命令
- python - 为什么 BucketedRandomProjectionLSH 在 pyspark 中的 100 万张图像上花费这么多时间?
- c# - Selenium C# 选择 UL IL 和标签值
- javascript - 如何以 JSON 格式 PHP 返回 Javascript 变量?
- regex - 验证 Codeigniter 中的输入是否具有安全性和空格字母数字和特定字符