2012-03-01 65 views
2

更新SQLite表我想交換SQLite中的Android問題與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); 
} 

特定列兩行的值如果我們假設OLDVAL = 10的newval = 20,那麼就應該是這樣的

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 

所以最終的答案應該是20和10,但它表明20和20

在我的邏輯失效,請幫幫我嗎?

謝謝...

+0

'==?有趣... – Selvin 2012-03-01 13:04:31

+0

我用=代替它,但結果仍然相同 – 2012-03-01 13:06:51

回答

1

在上次更新中出現了一個愚蠢的錯誤。取而代之的是

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

我應該寫'在SQL這

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

感謝上帝......