0

java sqlite insert very slow... my code is this

        for(int i = 1 ; i <= list.size(); i++ ) {
        query="INSERT OR REPLACE INTO highErrorProbabillityDB VALUES (?,?);";
        prep = localError.prepareStatement(query);
        prep.setString(1, list.get(i-1).get("ki"));
        prep.setInt(2, Integer.parseInt(list.get(i-1).get("ccolor")));
        prep.executeUpdate();
        }

The first column is primary key and On average, it takes time of about one to 150Milliseconds.

how to optimize this code?

user3853197
  • 39
  • 1
  • 3

2 Answers2

2

First of all, you could prepare statement only once:

query="INSERT OR REPLACE INTO highErrorProbabillityDB VALUES (?,?);";
prep = localError.prepareStatement(query);
for(int i = 1 ; i <= list.size(); i++ ) {
    prep.setString(1, list.get(i-1).get("ki"));
    prep.setInt(2, Integer.parseInt(list.get(i-1).get("ccolor")));
    prep.executeUpdate();
}
lpg
  • 4,784
  • 1
  • 13
  • 15
0

If you have SQLite version 3.7.11 or greater then you can do multiple inserts at once like this

INSERT OR REPLACE INTO highErrorProbabillityDB 
VALUES (1,4),
       (2,5),
       (3,6),
       ...

That way you only have to execute a single query.

juergen d
  • 186,950
  • 30
  • 261
  • 325