我在课堂上生成删除命令时遇到问题:
private String generateDeleteCommand() {
StringBuilder deleteCommand = new StringBuilder();
for (ForeignKey fk : exportedForeignKeys) {
deleteCommand.append("DELETE FROM ").append(fk.foreignTableName)
.append(" WHERE ").append(fk.foreignColumnName)
.append("=:").append(fk.primaryColumnName).append(";\n");
}
deleteCommand.append("DELETE FROM ").append(tableName)
.append(" WHERE ");
for (String key : primaryKeys.keySet()) {
deleteCommand.append(key).append("=:").append(key).append(" AND ");
}
deleteCommand
.delete(deleteCommand.length() - 5, deleteCommand.length());
deleteCommand.append(";");
System.out.println(deleteCommand);
return deleteCommand.toString();
}
在 phpmyadmin 中使用时,我得到的查询是有效的 - 但是当我尝试将它与 jdbc executeUpdate() 一起使用时,我得到 MySQLSyntaxError,即对于带有两个导出外键的表“trasy”查询看起来像:
DELETE FROM kursy WHERE ID_TRASY=19;
DELETE FROM przystanki WHERE ID_TRASY=19;
DELETE FROM trasy WHERE ID_TRASY=19;
例外:
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 'DELETE FROM przystanki WHERE ID_TRASY=19;
DELETE FROM trasy WHERE ID_TRASY=19' at line 2
查询之间是否有 \n 无关紧要。