如何减少 SQLite 表的 int 列

问题描述 投票:0回答:1

我的数据库中有一个表,其中包含三列:

Screen
Icon
Rank
,我正在尝试找到最干净的方法来实现以下目标:

我想找到所有行

WHERE Screen = "myScreen" and Rank > 5
,然后使排名比当前值少一,我通过以下函数中的 SQLite Manager 类在 Java 中执行此操作:

public void DeleteScreenIcon (int id, String screenName, int rank){
    int screenID = getScreenID(screenName);
    SQLiteDatabase db=this.getWritableDatabase();
    db.delete(isLookUp, colScreenID + "=" + screenID + " and " +colIconID+ "="+id, null);
    // HERE IS WHERE I NOW WANT TO DO THAT..
    db.execSQL("update "+ isLookUp +" set "+colRank+ "=" +colRank+ " -1 "+" where " + colScreenID +  "='"  +screenName + "' and " + colRank +">" +rank);
    db.close();
}
java android sqlite
1个回答
9
投票

此更新应该可以解决问题:

UPDATE myTable 
SET Rank = Rank - 1
WHERE Screen = "myScreen"
AND Rank > 5;
© www.soinside.com 2019 - 2024. All rights reserved.