文章内容

2019/7/4 10:34:07,作 者: 黄兵

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

最近后台的服务出现了如下报错内容:

 _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 '' at line 1")


出现问题的原因:

应该是在插入某些字段的时候,导致多一个‘而使sql语句没有闭合,导致插入失败。

通过日志分析以及不断测试,终于找到了执行错误的SQL语句:

SELECT t.Token,l.id FROM p l inner join tokenlist t on l.id = t.p_id where l.p=;

SQL查询报错截图如下:

跟上面报错提示差不多。


解决方案:

在执行SQL语句以前执行判断。


上面只是查询错误,但是相对于查询插入出现错误的机会更多一些,如果插入出现了错误应该如何处理呢?

insert = (
      "INSERT INTO %s" % name_tabela
      + " VALUES (DEFAULT, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" 
)
cursor.execute(insert, (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))

可以使用这种方式插入,避免有些语句没有闭合。

你也可以简化excute的第二个arg:

cursor.execute(insert, row[:15])
# or even this if the `row` has exactly 15 values
cursor.execute(insert, row)


参考资料:

1、_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax;)


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - 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

分享到:

发表评论

评论列表