Chandra Sekhar
Chandra Sekhar

Reputation: 19502

Issue with updating SQLite table in android

I want to swap values of two rows of a particular column in SQLite Android

public void updatePosition(int oldVal, int newVal){
    ContentValues updatedValue1 = new ContentValues();
    updatedValue1.putNull(ColName);
    db.update(TABLE_NAME, updatedValue1, ColName + "==" + newVal, null);
    ContentValues updatedValue2 = new ContentValues();
    updatedValue2.put(ColName, newVal);
    db.update(TABLE_NAME, updatedValue2, ColName + "==" + oldVal, null);
    ContentValues updatedValue3 = new ContentValues();
    updatedValue3.put(colName, oldVal);
    db.update(TABLE_NAME, updatedValue2, colName + " IS NULL", null);
}

If we assume oldVal = 10 and newVal = 20, then it should go like this

1st update : put null where value is 20 -- Now colName has 10 and null
2nd update : put 20 where value is 10   -- Now colName has 20 and null
3rd update : put 10 where value is null -- now colName has 20 and 10

so the final answer should be 20 and 10 but it shows 20 and 20.

Please help me where my logic fails?

Thanks...

Upvotes: 2

Views: 199

Answers (1)

Chandra Sekhar
Chandra Sekhar

Reputation: 19502

What a silly mistake in the last update. Instead of this

updatedValue3.put(colName, oldVal);
db.update(TABLE_NAME, updatedValue2, colName + " IS NULL", null);

I should write this

updatedValue3.put(colName, oldVal);
db.update(TABLE_NAME, updatedValue3, colName + " IS NULL", null);

Thank GOD...

Upvotes: 1

Related Questions