Pages

Android SQLite + Transactions

Running 1000's of insert/update/delete queries in SQLite can be very time consuming. But if you do them in a transaction it will be done quickly. Batch processing I guess.

This is how you do it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dh.getWritableDatabase();

db.beginTransaction();

try {
    // perform all insert/update/delete queries here

    db.setTransactionSuccessful();
} finally {
    db.endTransaction();
}

db.close();

Note: perform database operations in the background using AsyncTask.