Reputation: 475
I need to update only the first row of a specific sql query command
my code is:
SQLiteDatabase db = this.getWritableDatabase();
String strSQL = "UPDATE table SET column = something WHERE name = john AND age = 10";
db.execSQL(strSQL);
I have like 5 results with the same criteria of name=john and age of 10
this works to update the last item however I want it to update the first row result only of the WHERE clause. how can this be possible?
Upvotes: 0
Views: 1496
Reputation: 1123
First: (in your case) If you want to update only first row then add one more parameter in your table. It will help you to find that record uniquely
Second: Don't use update query like this
String strSQL = "UPDATE table SET column = something WHERE name = john AND age = 10";
do it like this instead
Upvotes: 0
Reputation: 8844
try this:
String strSQL = "UPDATE table SET column = something WHERE id IN (SELECT id FROM table WHERE name = john AND age = 10 LIMIT 1)";
Upvotes: 1
Reputation: 22811
Provided id is a PK, try
UPDATE table
SET column = something
WHERE id = (SELECT min(t2.id)
FROM table t2
WHERE t2.name = 'john' AND t2.age = 10)
Upvotes: 0