Android SQLite 递增整数字段从查询中读取

Android SQLite Incrementing Integer Field Read from Query

预先披露,这是一个学校项目。

我在 class 中有一个方法来管理我应用程序中 "quzzer" 功能的数据库,它旨在增加(或在一种情况下减少)SQLite 中的三个整数字段数据库。它需要独立于 "quizzing functions" 执行此操作,因此我需要先拉取数据,更改它,然后将其更新到数据库中。

数据库中的字段如下: "prof_level" - 唯一可接受的值是 1 到 4(含)。 "times_correct" - 只有正数。 "times_incorrect" - 只有正数。

我可以从数据库中提取数字,然后将它们递增 1,但是当我更新时,它们将数据库中的值递增 2,我不知道为什么。这是该方法的完整代码:

    public void updateCharacterProf(String table, String charToUpdate, boolean isIncreased){
    //get character from table
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    qb.setTables(table);
    String[] projection = {"prof_level", "times_correct", "times_incorrect"};
    Cursor c = qb.query(db, projection, "character=='" + charToUpdate + "'", null, null, null,
                            null, null);

    c.moveToFirst();

    //check to see if the prof level is at max/min
    int profLevel = c.getInt(0);
    int correctTimes = c.getInt(1);
    int incorrectTimes = c.getInt(2);

    //mod prof levels
    if (isIncreased){
        profLevel++;
        correctTimes++;
    }
    else{
        profLevel--;
        incorrectTimes++;
    }

    if (profLevel == 4 && isIncreased){
        profLevel = 4;
    }
    else if (profLevel == 1 && !isIncreased){
        profLevel = 1;
    }

    c.close();

    ContentValues values = new ContentValues();
    values.put("prof_level", profLevel);
    values.put("times_correct", correctTimes);
    values.put("times_incorrect", incorrectTimes);

    //update db
    db.update(table, values, "character=='" + charToUpdate + "'", null);

    db.close();
}

我希望这只是我不了解如何更新 SQLite 数据库的事情,但我迷失了 "var++ == +=2" 我现在得到的东西。

我发现问题是我自己造成的,我在调用它的对话框片段中意外触发了两次调用此数据库更新的回调(一次在 onClick 方法中,一次在生命周期 onDismiss 覆盖中。 ).

修复这个在另一个不同的数据库相关的事情中发生的错误,为我解决了这个问题。