phramusca
phramusca

Reputation: 123

Java DELETE PreparedStatement returns 0 whereas manual query works

I face a weird behavior (or I am missing somtehing). Below query does return 0 for all batch entries, whereas it does delete when I run a single query using Sqliteman. Any idea what's wrong ? Thanks

this.stDeleteDeviceFile = con.prepareStatement("DELETE FROM deviceFile "
                + "WHERE idFile=(select idFile FROM file F JOIN path P ON P.idPath=F.idPath WHERE (P.strPath || F.name)=?) "
                + "AND idDevice=?");

public boolean deleteDeviceFiles(ArrayList<String> relativeFullPaths, int idDevice) {
    try {
        if(relativeFullPaths.size()>0) {
            con.setAutoCommit(false);
            int[] results;
            for(String relativeFullPath : relativeFullPaths) {
                this.stDeleteDeviceFile.setString(1, relativeFullPath);
                this.stDeleteDeviceFile.setInt(1, idDevice);
                this.stDeleteDeviceFile.addBatch();
            }
            long startTime = System.currentTimeMillis();
            results = this.stDeleteDeviceFile.executeBatch();
            con.commit();
            long endTime = System.currentTimeMillis();
            Main.logger.log(Level.FINEST, "stDeleteDeviceFile DELETE // {0} // Total execution time: {1}ms", new Object[]{results.length, endTime-startTime});   //NOI18N

            //Analyse results
            int result;
            for(int i = 0; i < results.length; i++) {
                result = results[i];
                if(result<0) {
                    Main.logger.log(Level.SEVERE, "stDeleteDeviceFile, relativeFullPath=\"{0}\", idDevice={1}, result={2}", new Object[]{relativeFullPaths.get(i), idDevice, result});  //NOI18N
                }
            }
        }
        return true;

    } catch (SQLException ex) {
        Popup.error("deleteDeviceFiles()", ex);  //NOI18N
        return false;
    }
}

Upvotes: 0

Views: 59

Answers (1)

defectus
defectus

Reputation: 1987

The setString and setInt methods on the prepared statement use both the same index 1 (leaving the second placeholder ? - idDevice - unsubstituted). Hope that helps.

Upvotes: 3

Related Questions