简体   繁体   中英

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

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

For some reason it is getting completely tripped up at the string placeholder (%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.

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM