I use a SQLite Database in my app. For a specific action, I want to remove multiple (could be quite some, up to approx. a hundred) entries in a specific table. I have tried doing this by calling deleteStuffLink()
as many times as needed in a for
loop, and adding the loop inside the deleteStuffLink()
method. Both of those ways resulted in a 3 second hang for only 15 items.
What is causing this slow behavior, and how can I resolve it?
Normal delete method
for (String s : nameArray) { deleteStuffLink(s); }
public void deleteStuffLink(String name) {
SQLiteDatabase db;
db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_LinkTable
+ " WHERE " + LT_COLUMN_NAME + "=\"" + name + "\";");
db.close();
}
With loop incorporated
public void deleteAllStuffLink(ArrayList<String> nameArray) {
SQLiteDatabase db;
db = getWritableDatabase();
for (String s : nameArray) {
db.execSQL("DELETE FROM " + TABLE_LinkTable
+ " WHERE " + LT_COLUMN_NAME + "=\"" + s + "\";");
}
db.close();
}
As CommonsWare comment,
public void deleteAllStuffLink(ArrayList<String> nameArray) {
SQLiteDatabase db;
db = getWritableDatabase();
db.beginTransaction();
for (String s : nameArray) {
db.execSQL("DELETE FROM " + TABLE_LinkTable
+ " WHERE " + LT_COLUMN_NAME + "=\"" + s + "\";");
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
}