MySQL python connector UPDATE error: check right syntax to use near %s. What is going wrong?

MySQL python connector UPDATE error: check right syntax to use near %s. What is going wrong?

这应该相对简单快捷:

当我 运行 这个在我的 python IDE

mycursor.executemany("UPDATE table42 SET date = %s ", [('2020-05-11')])

由于某种原因,它在字符串占位符 (%s) 处完全出错。我使用 executemany 的原因是因为很快该字符串将被使用 today.strftime('%Y-%m-%d') 的变量替换,所以我需要它更灵活。

错误如下: mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1

如果有人能帮助我找出我做错了什么,将不胜感激。

日期字符串后需要一个逗号,这样列表就是一个元组列表(创建元组的是逗号,而不是括号)。

mycursor.executemany("UPDATE table42 SET date = %s ", [('2020-05-11',)])

这是因为DB API需要参数

be provided as sequence or mapping

(从技术上讲,一个字符串是一个序列,但在这里它的长度是错误的。mysql-connector 在任何情况下都拒绝一个字符串,我认为 pymysql 会接受一个IIRC。但将参数设为元组绝对是最可移植的编码方式)