2

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...

1
  • I replaced it with =, but still same result Commented Mar 1, 2012 at 13:06

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.