Java DELETE PreparedStatement返回0而手动查询有效

时间:2014-06-11 13:21:55

标签: java sqlite

我面对一种奇怪的行为(或者我错过了somtehing)。对于所有批处理条目,下面的查询确实返回0,而当我使用Sqliteman运行单个查询时,它确实会删除。 知道什么是错的吗?感谢

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;
    }
}

1 个答案:

答案 0 :(得分:3)

预准备语句中的setStringsetInt方法使用相同的索引1(保留第二个占位符? - idDevice - unsubstituted)。 希望有所帮助。