There are multiple methods on the whole network to solve You have an error in your SQL syntax; check the manual that corresponds to your MySQL server

1. Reproduce the error


Today, when debugging the low-code interface, the following error is suddenly reported:

insert image description here

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.SQLSyntaxErrorExceptioncan be seen that this is SQLa grammatical error reported.

Find the printed SQLlog, as shown in the following figure:

insert image description here

MySQLCopy the statement in the red box to MySQLthe console for execution, as shown in the following figure:

insert image description here

I'm pretty sure insert into project(remark,desc,name,is_deleted ) VALUES('测试哈','测试哈','测试项目',1 );, MySQLthere is no syntax error in that statement.

That can only consider whether there are conflicting MySQL关键字fields in the statement.

Take a closer look, descit is MySQLthe 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 descand MySQLin my SQL statement descconflict.

Therefore, I can modify it in the following two ways:

  1. method one

Modify my descfield to other MySQLfields that do not conflict with keywords, such as noteetc.

  1. Method Two

Add backticks (`) to the above MySQLstatement , 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)

insert image description here

Therefore, in our actual development, it is best to add backticks (`) to fields such as adding, modifying, and querying.

Avoid MySQLkeyword conflicts with your own, thereby reducing unnecessary trouble.

4. Other ways to resolve this error


My mistake is because in insertthe statement, there is MySQLa 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.

  1. Single quotes and back-single quotes are indistinguishable

javaSingle 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');");

insertDataIt is responsible for executing this SQLstatement, and he finally returns the number of affected items, so don't worry about it.

in:

  • The column names are nowtimeand data(here use backsingle quotes)

  • The value written to the database is current_timethe ampersand character A(single quotes here).

  1. Check if true is a syntax error

This can usually be seen.

  1. 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 testand 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.

SQLTherefore, it is better to write short sentences as one sentence to reduce bugproduction efficiency.

Guess you like

Origin blog.csdn.net/lvoelife/article/details/130108739