Proper syntax for updating a row in Android with SQLite
Ok, I have two Android projects both running the same API level. In one, to update a SQLite row I am using:
db.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" + rowId, null);
In the other I am using:
db.update(DATABASE_TABLE, updateValues, FIELD_COLLECTION_ID + "=?", new String[] {collectionId});
They both work. Except that in the second project, if I change the syntax to match the first one, (i.e. remove the question mark from the where clause and set whereArgs
to null), it returns 0 rows affec开发者_StackOverflow社区ted. Is there a reason for this? Im thinking of sticking to the second syntax because apparently it reduces the possibility of SQL injection but I thought I would throw this one out there and see if anyone had a similar experience.
Also interesting is that the documentation doesn't specify what whereArgs is for, its just there: SQLiteDatabase:update()
When you remove the question mark, are you replacing it with the value of collectionId
?
The second syntax means that SQLite can reuse the same cached copy of the query for any number of different values of rowId
or collectionId
, rather than having a completely different query each time it's invoked. Saves lots of resources.
精彩评论