简体   繁体   English

MySQL python 连接器更新错误:检查正确的语法以在 %s 附近使用。 出了什么问题?

[英]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.

相关问题 使用 python 创建 Mysql 数据库 - 不断收到 1064 错误(...在 '%s' 附近使用正确的语法) - Creating Mysql database with python - keep getting 1064 error (… right syntax to use near '%s' ) mysql.connector.errors.ProgrammingError: 1064 (42000): 你的 SQL 语法有错误; 在第 1 行的“)”附近使用正确的语法 - mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; for the right syntax to use near ')' at line 1 修复“查看与您的 MySQL 服务器版本相对应的手册,以获取在 '%s 附近使用的正确语法” - FIX “Check the manual that corresponds to your MySQL server version for the right syntax to use near '%s” 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 \'keyword")\' 附近使用的正确语法 - 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 \'keyword")\' 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 '*) 附近使用的正确语法 - 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 '*) 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 ') 附近使用的正确语法 - 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 ') “你的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在''第1行'附近使用正确的语法 - “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 '' at line 1” 警告:您的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法 - Warning: 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 _mysql_exceptions错误(1064,“检查与MySQL服务器版本对应的手册,以便在'default'附近使用正确的语法)VALUES - _mysql_exceptions error(1064, "check the manual that corresponds to your MySQL server version for the right syntax to use near 'default) VALUES 使用 MySQL 数据库中的 Python 选择数据时使用“%s”附近错误的正确语法 - Correct syntax to use near '%s' error while selecting data using Python from MySQL database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM