gpt4 book ai didi

android - 在 android 中更新 SQLite 表的问题

转载 作者:行者123 更新时间:2023-11-29 02:06:54 26 4
gpt4 key购买 nike

我想在 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);
}

如果我们假设 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。

请帮我解决我的逻辑问题?

谢谢...

最佳答案

上次更新中出现了多么愚蠢的错误。而不是这个

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

我应该写这个

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

感谢上帝...

关于android - 在 android 中更新 SQLite 表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9516522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com