Reputation: 571
I have written this code inside a servlet to delete certain records from three tables.
Note: two of these tables are myisam table for Full-text search reason and other one is innodb table.
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);
I get the following error. I cant see any syntax error in the query I have write
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)
Upvotes: 1
Views: 1390
Reputation: 305
I don't have a mySql db to test it, but I think the problem might be that it cannot determine which proposal_id it should use (having 3 tables). have you tried expliciting the join and specifying a table name before proposal_id?
something like
"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
still, I agree with the others, 3 separate delete would be a better option..
Upvotes: 0
Reputation: 57388
Have you tried deleting from the three tables separately (possibly inside a transaction?)
Delete from multiple tables requires a table reference (e.g. a JOIN). It was born to delete linked records.
So you should try
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 = ...
Upvotes: 0
Reputation: 920
Delete from can delete data from table like this but i don't know whether if can delete data like you post.
"DELETE FROM proposal WHERE proposal_id="+id
"DELETE FROM suitable_fo WHERE proposal_id="+id
"DELETE FROM skill_need WHERE proposal_id="+id
And before you invoke this, make sure param id is being checked to avoid sql injection.
Upvotes: 1