Search code examples
androidsqliteandroid-sqlitedelete-row

Android SQLite "random" rows seem inaccessible


I have a database with 4 columns. Everything works fine except for the fact that random rows remain inaccessible. I cannot seem to update or delete them. I've used the right code:

DELETION CODE:

boolean deleted=db.delete(TABLE_NAME, KEY_BLAH + "=" + alarm, null) > 0;

CODE FOR UPDATE:

db.update(TABLE_NAME, args, KEY_BLAH + "=" + blah, null)

The deletion code returns false and the update code returns 0 for some rows, which are arbitrary. The rows are implemented into a ListView. Any changes on the ListView elements should be reflected in the SQLite Database. Most of the times the code works. It doesn't seem to work for random rows at random points though. I do not think this is SDK related. I've tried it on ICS, GB and JB.

I've also tried deleting the row and inserting it. Obviously this wouldn't work because my delete statement doesn't seem to work. The logic for this was, if the update wasn't possible it would delete the row and insert the row with the same row id and different data. So, the deleted variable was the condition for an if-block which didn't get executed.

EDIT

Example:

1 1414 000 off

When I click on a few buttons in the list item.

It should become

1 1414 064 off

Now, this works most of the times except for some random rows as I said before.


Solution

  • If you (indirectly) execute an SQL statement like this:

    UPDATE MyTable SET x = '064' WHERE blah = 1414
    

    then the values in the blah column are compared with the number 1414.

    When using strings in SQL statements, you should always use parameters to avoid formatting problems like this (and SQL injection attacks):

    db.update(TABLE_NAME, args, KEY_BLAH + "= ?", new String[] { blah })