[英]MySQL python connector UPDATE error: check right syntax to use near %s. What is going wrong?
this should be relatively quick and easy:这应该相对快速和容易:
When I run this in my python IDE当我在我的 python IDE 中运行它时
mycursor.executemany("UPDATE table42 SET date = %s ", [('2020-05-11')])
For some reason it is getting completely tripped up at the string placeholder (%s).由于某种原因,它在字符串占位符 (%s) 处完全被绊倒了。 The reason I am using executemany is because soon that string will be replace by a variable using today.strftime('%Y-%m-%d')
so I need it to be more flexible.我使用 executemany 的原因是因为很快该字符串将被使用today.strftime('%Y-%m-%d')
的变量替换,所以我需要它更灵活。
The error is as follows: 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
错误如下: 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
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
If someone could help me figure out what I'm doing wrong it would be greatly appreciated.如果有人可以帮助我找出我做错了什么,将不胜感激。
There needs to be a comma after the date string, so that the list is a list of tuples (it's commas that create tuples, not the parentheses).在日期字符串之后需要有一个逗号,以便列表是一个元组列表(创建元组的是逗号,而不是括号)。
mycursor.executemany("UPDATE table42 SET date = %s ", [('2020-05-11',)])
This is because the DB API requires that parameters这是因为DB API需要该参数
be provided as sequence or mapping作为序列或映射提供
(Technically a string is a sequence, but here it would be of the wrong length. mysql-connector rejects a string in any case I think, pymysql would accept one in this IIRC. But making the parameters a tuple is definitely the most portable way to code this) (从技术上讲,字符串是一个序列,但在这里它的长度是错误的。无论如何我认为 mysql-connector 拒绝一个字符串,pymysql 会在这个 IIRC 中接受一个字符串。但是将参数设为元组绝对是最便携的方式编码这个)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.