SQL报错(一)| You have an error in your SQL syntax; check the manual that

961 阅读1分钟
文章已参与[新人创作礼]活动,一起开启掘金创作之路
微信公众号:纯洁的麦田。如有问题,请后台留言,反正我也不会听。 

前言

或多好少,你肯定为sql报错犯愁

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: java.sql.SQLSyntaxErrorException: 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 62
### The error may exist in file [D:\Users\IdeaProjects\parent\exact-service\target\classes\mapper\ProjectVerStTestCaseDefMapper.xml]
### The error may involve com.mapper.ProjectVerStTestCaseDefMapper.insertBatch-Inline
### The error occurred while setting parameters

假如你遇到以上这个报错不用质疑,肯定就是sql出了问题,那么怎么辨别,看看报错的sql

insert into project_ver_st_test_case_def         (proj_ver_st_rev_id, proj_ver_id, test_case_id,         mdl_id, seq, preffix_cond,         suffix_cond, test_step, status,         expect_result, gen_type, test_case_abbr,         is_confirm, remark, created_by,         create_date, modified_by, modify_date) value  (  ?,   ?,      ?,     ?,     ?,  ?,     ?,  ?,  ?,  ?,  ?,?,  ?, ?,  ?,  ?,   ?, )  ,  (  ?,  ?,  ?,  ?,   ?, ?,   ?, ?,    ?, ?,    ?, ?, ?,   ?, ?, ?, ?,) , 

看看sql报错日志

Caused by: java.sql.SQLSyntaxErrorException: 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 62
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
	at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:370)
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:47)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
	at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doUpdate(MybatisSimpleExecutor.java:56)
	at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
	at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.update(MybatisCachingExecutor.java:85)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
	at com.sun.proxy.$Proxy195.update(Unknown Source)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
	... 70 more 

总结:

其实你这个时候就已经看出来了,就是缺少多一个逗号 一般这种报错就是有两种情况:

  • sql字段错误
  • 多了、少了逗号
  • 在英文键盘输入环境下,按图示按钮输入反引号 `

英文输入反引号情况可以看相关文,献其实这个问题就是语法上的错误,在MySQL中,为了区分MySQL的关键字与普通字符,MySQL引入了一个反引号。
详情见:dev.mysql.com/doc/refman/…