4

I'm using a fairly simple command to try to delete rows in a SQLite database. However, after successfully running the command, the rows remain in the DB!?

SQLiteDatabase dbinst = mydb.getWritableDatabase();

try
{
  dbinst.beginTransaction();
  int del = dbinst.delete("sms", "wassent = ? or waserror = ? or wasaborted = ?", new String[] {"1", "1", "1"} );   
  clog.debug("Rows deleted: " + del);
  dbinst.setTransactionSuccessful();
}
catch(Exception e)
{
  clog.error(e.toString(),e);
}

dbinst.close();

The logger says "rows deleted: 3", and no exception arises. However, when doing a query immediately afterwards, the rows are still there?

Any obvious thing I'm doing wrong here?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Joel Palmius
  • 191
  • 1
  • 5

1 Answers1

2

You have to call endTransaction to actually commit the changes. More info can be found here: beginTransaction() documentation

Eugene S
  • 3,092
  • 18
  • 34
  • Say, does it help in any way to actually use a transaction for a simple deletion? And how about a single deletion that has multiple rows to delete from? Any better in performance or in thread-safety? – android developer Mar 28 '15 at 17:15
  • @androiddeveloper Transactions are useful if you want to update several tables atomically (either all of the updates work or they all fail, no mixed results). Also, if your application can't handle the user pulling the battery in the middle of a table update then use transactions. – Eugene S Mar 31 '15 at 23:56
  • makes sense. but is it useful in case I know a deletion operation would be only for a single row? what about multiple rows? Would using deletion without transaction be dangerous in terms of thread-safety ? I mean, is this "db.delete(...)" thread safe ? – android developer Apr 01 '15 at 08:57
  • @androiddeveloper All db interactions are thread safe by default. The usefulness of transactions to your application depends on the factors I've laid out in my previous comment. If you're just updating a single row, then transactions don't give you any added benefit. If you're worried about performance, then you're probably overthinking it. – Eugene S Apr 02 '15 at 12:45
  • So no matter how complex the db operation is, it's thread safe ? OK, thank you. – android developer Apr 02 '15 at 12:49
  • I had beginTransaction() and endTransaction, but was coming up with the same problem. I added db.setTransactionSuccesful() and that solved it. – flobacca Jan 02 '16 at 02:43