mysql升级版本后正常执行的sql报错:Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

版权声明:本文为博主原创文章,转载请注明出处,尊重劳动成果,谢谢~ https://blog.csdn.net/zhanglf02/article/details/88919615

业务场景是这样的:本地测试接口时用的本地mysql数据库是低版本的5.1.55-community,没有问题。但是上到测试环境后,执行同样的接口请求,却报了sql异常。测试环境的mysql库是1.8高版本的,感觉问题出在版本上。加上本地测试没问题,可以肯定是数据库的问题。剩下的就是排查数据库哪方面的问题了。
报错信息如下;

Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ', status, tid, create_time, \n    update_time\n   \n    from class_task\n    where  ' at line 3\n
### The error may exist in class path resource [mybatis/ClassTaskMapper.xml]\n### The error may involve defaultParameterMap\n### The error occurred while setting parameters\n
### SQL: select           id, room_id, task_code, task_title, task_type, thumbnail, rank, status, tid, create_time,      update_time         from class_task     where  task_type = 1     and tid = ?     and room_id = ?\n
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ', status, tid, create_time, \n    update_time\n   \n    from class_task\n    where  ' at line 3\n; bad SQL grammar []; 
nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ', status, tid, create_time, \n    update_time\n   \n    from class_task\n    where  ' at line 3

首先我想到的是可能测试环境的表和我本地的表不一样,就导出了测试环境的表和我本地比较,发现除了最后的编码方式选择有差异外,别的没啥不同。
在这里插入图片描述
我就想是不是我sql里面有中文空格啥的问题。就根据报错的信息找到对应的mapper文件的sql语句,把传的参数赋值进去,在本地的库里执行,正常运行,然后放到测试环境执行却报错了。如下图:
在这里插入图片描述
根据错误代码为1064 ,最终发现是最有可能的一个问题:表中的字段和MySQL中的关键字冲突。然后搜索rank字段,发现果然rank是mysql的排序函数,不是关键字却是方法。这就是问题所在!

找到问题后,就临时更改掉代码中的字段rank,后重新部署发现错误解决!

这里总结一下: 如果报:The error may involve defaultParameterMap\n### The error occurred while setting parameters除了中文字符在sql中存在外,和确实参数赋值的表达式不对这些都排出外,还应该考虑sql的字段中是不是有关键字段或者存在的字段就是个mysql的函数?我这里的字段rank就是个坑。引以为戒。

猜你喜欢

转载自blog.csdn.net/zhanglf02/article/details/88919615
今日推荐