在我的 java 项目中,我需要检查表中是否存在一行。如果存在,我需要更新;如果没有,我需要创建它。执行此操作的 Sql 语法应该是:
IF EXISTS(SELECT * FROM table1 WHERE column4='"+int4+"' AND column5='"+int5+"') "
+"BEGIN "
+ "UPDATE table1"
+ "SET column1='"+int1+"', column2='"+int2+"' "
+ "WHERE column4='"+int4+"' and column5='"+int5+"' "
+ "END "
+ "ELSE"
+ "INSERT INTO table1 (column1, column2, column4, column5, column3) "
+ "VALUES ('" + int1 + "',"
+ "'" + int2 + "',"
+ "'" + int4 + "',"
+ "'" + int5 + "',"
+ "'" + int3 +"');
其中int1, int2, int3, int4, int5
是整数值。好吧,如果我把这段代码放在我的 java 编译器上,我的 Sql 语法错误:
com.mysql.jdbc.exceptions.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 'IF EXISTS(SELECT * FROM table1 WHERE column4='1' AND column5='0') BEGIN UPDATE' at line 1
但我看不到错误