what i need is a query to delete all ids except those i have specified. therefore i have such a query in spring:
private final String SQL_Clear_Deleted_Options = "DELETE FROM vote_votes WHERE poll_id=? AND option_id <> ?";
i'm using jdbcTemplate and batchUpdate to do so. i also used <> operator to indicate NOT IN clause. this is my code:
public void clearDeletedOptions(int id) {
int[] argTypes = { Types.INTEGER, Types.INTEGER };
List<Object[]> batchArgs = new ArrayList<>();
for (int i:ids) {
batchArgs.add(new Object[]{id, i});
}
jdbcTemplate.batchUpdate(SQL_Clear_Deleted_Options, batchArgs, argTypes);
}
in above code ids is a list of integers that indicates option_id s in the query. i wonder why it works opposite and deletes all ids given! every thing looks fine and batchArges contains pairs (poll_id,option_id) indicating a particular poll_id and option_ids that should not be removed.
what is the problem?
This is because, each not equals will delete rest of the records. One of the solution is to use
jdbcTemplate.execute
by dynamically creating sql.