简体   繁体   English

Python MySQL 语法错误

[英]Python MySQL Syntax Error

I've been trying to work out where I've been going for most of the day, and still can't work it out, been staring at it too long and I'm sqill very early in my learning of Python & MySQL.我一直在努力弄清楚我一天中的大部分时间都去哪里了,但仍然无法解决,盯着它太久了,我很早就开始学习 Python 和 MySQL。

The query I've built is:我建立的查询是:

query = "UPDATE `db`.`%s" % table + "` SET %s" % table + "`.`%s" % field + "` = `%s" % daychangeperc + "` WHERE (`db`.`%s" % table + "`.`id` = %s" % rowid +") LIMIT 1;"

The error I'm getting is:我得到的错误是:

_mysql_exceptions.ProgrammingError: (1064, "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 ' . DayChange = '-0.00736251627767' WHERE ( outofthe_finance . test1 . id` = 1) ' at line 1") _mysql_exceptions.ProgrammingError: (1064, "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 ' . DayChange = '-0.00736251627767' WHERE ( outofthe_finance . test1 . id` = 1) ' 在第 1 行")

Anyone able to point out where I'm going wrong?谁能指出我哪里出错了? I'm sure it's probably obvious for many.我敢肯定,这对许多人来说可能是显而易见的。

Thanks in advance.提前致谢。

That's pretty messy, perhaps try doing it like this instead:这很混乱,也许可以尝试这样做:

query = """UPDATE db.%s SET %s.%s = %s WHERE db.%s.id = %s LIMIT 1""" % (table,table,field,daychangeperc,table,rowid)

First, here is your query re-written so that all of the arguments for your format string come at the end:首先,这是您重新编写的查询,以便您的格式字符串的所有 arguments 都位于末尾:

query = "UPDATE `db`.`%s` SET %s`.`%s` = `%s` WHERE (`db`.`%s`.`id` = %s) LIMIT 1;" \
 %(table,table,field,daychangeperc,table,rowid)

When you look at it this way, you can see that after the SET keyword you have some unbalanced back-ticks .当你这样看时,你可以看到在SET关键字之后你有一些不平衡的反引号 I think this is the issue, and is hard to see because of how you wrote your string.我认为这是问题所在,并且由于您编写字符串的方式而很难看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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