Mysql语法错误:删除记录

时间:2012-07-06 12:30:57

标签: mysql syntax-error sql-delete

我已在servlet中编写此代码以从三个表中删除某些记录。

注意:其中两个表是表,用于全文搜索原因,另一个表是表。

int id = Integer.parseInt(request.getParameter("id"));
try {
  Statement stmt = conn.createStatement();
  stmt.executeUpdate("DELETE FROM proposal, suitable_for,skill_need WHERE proposal_id="
                     +id);

我收到以下错误。我无法在我写的查询中看到任何语法错误

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 'where proposal_id=12' at line 1
at sun.reflect.GeneratedConstructorAccessor14.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2677)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1748)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1665)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
at Lucene.Core.indexer.Action.doDelete(Action.java:184)
at Lucene.Core.indexer.Action.doGet(Action.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

4 个答案:

答案 0 :(得分:1)

删除可以从这样的表中删除数据,但我不知道是否可以删除你发布的数据。

"DELETE  FROM proposal WHERE    proposal_id="+id
"DELETE  FROM suitable_fo WHERE    proposal_id="+id
"DELETE  FROM skill_need WHERE    proposal_id="+id

在调用它之前,请确保正在检查param id以避免sql注入。

答案 1 :(得分:0)

您是否尝试过分别从三个表中删除(可能在交易中?)

从多个表中删除需要表引用(例如JOIN)。它诞生于删除链接记录。

所以你应该试试

 DELETE FROM proposal, suitable_for, skill_need
 FROM proposal JOIN suitable_for ON (proposal.proposal.id = suitable_for.proposal_id)
 JOIN skill_need ON (skill_need.proposal.id = proposal.proposal.id)
 WHERE proposal.proposal_id = ...

答案 2 :(得分:0)

尝试使用executeQuery而不是executeUpdate。

答案 3 :(得分:0)

我没有mySql数据库来测试它,但我认为问题可能是它无法确定它应该使用哪个proposal_id(有3个表)。您是否尝试过显示连接并在proposal_id之前指定表名?

等等 "DELETE FROM proposal, suitable_for,skill_need WHERE proposal.proposal_id= suitable_for.proposal.proposal_id AND proposal.proposal_id=skill_need.proposal_id and proposal.proposal_id="+id

仍然,我同意其他人,3个单独的删除将是一个更好的选择..