Article directory
1. Reproduce the error
Today, when debugging the low-code interface, the following error is suddenly reported:
ie 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 'desc,name,is_deleted ) VALUES('测试哈','测试哈','测试项目',1 )' at line 11
.
Therefore, check the detailed error information reported by the console, as shown in the following figure:
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 'desc,name,is_deleted ) VALUES('测试哈','测试哈','测试项目',1 )' at line 1
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.executeUpdateInternal(ClientPreparedStatement.java:1092)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1348)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeUpdate(DruidPooledPreparedStatement.java:241)
at com.test.AppModelPageService.addData(AppModelPageService.java:904)
at com.test.AppModelPageService$$FastClassBySpringCGLIB$$81597048.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.test.AppModelPageService$$EnhancerBySpringCGLIB$$40f27572.addData(<generated>)
at com.sugon.cloud.lowcode.controller.AppModelPageController.AddData(AppModelPageController.java:125)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
......
2. Analysis errors
It java.sql.SQLSyntaxErrorException
can be seen that this is SQL
a grammatical error reported.
Find the printed SQL
log, as shown in the following figure:
MySQL
Copy the statement in the red box to MySQL
the console for execution, as shown in the following figure:
I'm pretty sure insert into project(remark,desc,name,is_deleted ) VALUES('测试哈','测试哈','测试项目',1 );
, MySQL
there is no syntax error in that statement.
That can only consider whether there are conflicting MySQL关键字
fields in the statement.
Take a closer look, desc
it is MySQL
the keyword that indicates descending order, as shown in the following code:
select * from product order by price desc;
3. Fix bugs
insert into project(remark,desc,name,is_deleted ) VALUES('测试哈','测试哈','测试项目',1 );
From the above analysis, we can see that the keywords in desc
and MySQL
in my SQL statement desc
conflict.
Therefore, I can modify it in the following two ways:
- method one
Modify my desc
field to other MySQL
fields that do not conflict with keywords, such as note
etc.
- Method Two
Add backticks (`) to the above MySQL
statement , as shown in the following code:desc
mysql> insert into project(remark,`desc`,name,is_deleted ) VALUES('测试哈','测试哈','测试项目',1 );
Query OK, 1 row affected (0.01 sec)
Therefore, in our actual development, it is best to add backticks (`) to fields such as adding, modifying, and querying.
Avoid MySQL
keyword conflicts with your own, thereby reducing unnecessary trouble.
4. Other ways to resolve this error
My mistake is because in insert
the statement, there is MySQL
a field that conflicts with the keyword, and the problem can be solved by adding back single quotation marks to the conflicting field.
If your error is not caused by this reason, you can refer to the following solutions.
- Single quotes and back-single quotes are indistinguishable
java
Single quotes (') are used for key values, and single quotes (`) are used for column names, such as
ed.insertData("insert into information(`nowtime`,`data`) values(current_time,'A');");
insertData
It is responsible for executing this SQL
statement, and he finally returns the number of affected items, so don't worry about it.
in:
-
The column names are
nowtime
anddata
(here use backsingle quotes) -
The value written to the database is
current_time
the ampersand characterA
(single quotes here).
- Check if true is a syntax error
This can usually be seen.
- Check if the statement forgets spaces
The following error is reported:
int result = ed.insertData("insert "+"into test"
+ "values(1) ");
It is no problem to combine the code into one sentence and execute it, as shown below:
int result = ed.insertData("insert into test values(1) ");
There is no space between the first sentence test
and the following values
, so an error will naturally be reported.
Readers should pay attention to whether you have also forgotten the space. This error often occurs when changing lines.
SQL
Therefore, it is better to write short sentences as one sentence to reduce bug
production efficiency.