android sqlite:获取错误的值

时间:2012-09-29 23:06:43

标签: android sqlite

目前我在android中使用sqlite数据库遇到了很多麻烦。这一次,真的很奇怪。

每当我输入某个活动时,我想显示最新的三个条目,例如“团队1与团队2目标 - 目标”。奇怪的是,数据库返回错误的值。让我们从当前的数据库内容开始:

enter image description here

这里我的问题很重要的是列“id”,“goals1”和“goals2”。现在如你所见,最近的三场比赛是“4:dfsadf vs. asf 3-2”,“3:df vs. 3-2”和“2:dfa vs. dfas 2-0”。在我的活动中,以下是显示:

enter image description here

如您所见,客场球队目标的目标是错误的(并且条目的ID似乎被设置为客场目标)。 我将这三个按钮的文本设置为:

entries = datasource.getLatestFootballEntry(challengeName);
button = (Button) findViewById(R.id.bt_overview_latest1);
            entry = entries.elementAt(0);
            buttonText = entry.date + ": " + entry.team1 + " vs. " + entry.team2 + " " + entry.goals1 + "-" + entry.goals2;
            button.setText(buttonText);
            button.setClickable(true);

            button = (Button) findViewById(R.id.bt_overview_latest2);
            entry = entries.elementAt(1);
            buttonText = entry.date + ": " + entry.team1 + " vs. " + entry.team2 + " " + entry.goals1 + "-" + entry.goals2;
            button.setText(buttonText);
            button.setClickable(true);

            button = (Button) findViewById(R.id.bt_overview_latest3);
            entry = entries.elementAt(2);
            buttonText = entry.date + ": " + entry.team1 + " vs. " + entry.team2 + " " + entry.goals1 + "-" + entry.goals2;
            button.setText(buttonText);
            button.setClickable(true);

我在这里获得具体条目:

public Vector<FootballEntry> getLatestFootballEntry(String challengeName){
        Vector<FootballEntry> entries = new Vector<FootballEntry>();
        Cursor cursor = database.query(CustomSQLiteHelper.TABLE_FOOTBALL, allColumns2, null, null, null, null, null);
        cursor.moveToLast();
        int i=0;
        while(i<3 && !cursor.isBeforeFirst()){
            if(checkIfCorrectChallengeName(cursor, challengeName)){
                entries.add(cursorToFootballEntry(cursor));
                i++;
            }
            cursor.moveToPrevious();
        }
        return entries;
    }

现在奇怪的部分来了。当我在按钮上设置文本的方法中进行以下更改时,一切都很好。

button = (Button) findViewById(R.id.bt_overview_latest1);
            datasource.open();
            entry = datasource.getSpecificFootballEntry(entries.elementAt(0).id);
            datasource.close();
            buttonText = entry.date + ": " + entry.team1 + " vs. " + entry.team2 + " " + entry.goals1 + "-" + entry.goals2;
            button.setText(buttonText);
            button.setClickable(true);

现在显示

enter image description here

这是我的cursorToFootballEntry方法

private FootballEntry cursorToFootballEntry(Cursor cursor){
    FootballEntry entry = new FootballEntry();
    entry.id = cursor.getLong(0);
    entry.challengeName = cursor.getString(1);
    entry.date = cursor.getString(2);
    entry.home = cursor.getInt(3);
    entry.platform = cursor.getString(4);
    entry.team1 = cursor.getString(5);
    entry.stars1 = cursor.getDouble(6);
    entry.team2 = cursor.getString(7);
    entry.stars2 = cursor.getDouble(8);
    entry.goals1 = cursor.getInt(9);
    Log.i("dfasdf", "IN CURSOR TO FOOENRTY  " + cursor.getInt(9) + "-" + cursor.getInt(10) + "   id: " + cursor.getLong(0));
    entry.goals2 = cursor.getInt(10);
    entry.finished = cursor.getInt(11);
    return entry;
}

如您所见,我在日志中打印了一些信息(即团队目标的两个值)。输出

09-29 22:50:18.191: I/dfasdf(7039): IN CURSOR TO FOOENRTY  3-4   id: 4
09-29 22:50:18.191: I/dfasdf(7039): IN CURSOR TO FOOENRTY  3-3   id: 3
09-29 22:50:18.201: I/dfasdf(7039): IN CURSOR TO FOOENRTY  2-2   id: 2
09-29 22:50:18.241: I/dfasdf(7039): IN CURSOR TO FOOENRTY  3-2   id: 4

所以基本上,ID 4的条目被读出两次,但有两个不同的离开目标值。是什么导致这个?我的意思是,无论我一次获得3场比赛还是单场比赛3次都没有区别,只要它们是相同的游戏,显然必须是(看到id)。获得单个游戏总是返回正确的值。如果我得到所有游戏(需要在不同的活动中),同样的事情也会发生。在数据库创建声明中,只有一个值设置为“自动增量”,而对于_id则没有其他值。

现在要“证明”这个,我输入了一个新条目,得分1-0:以下是显示的(设置文本的第二个变体,意味着第一个是正确的)

enter image description here

日志

09-29 23:02:13.281: I/dfasdf(7039): IN CURSOR TO FOOENRTY  1-5   id: 5
09-29 23:02:13.291: I/dfasdf(7039): IN CURSOR TO FOOENRTY  3-4   id: 4
09-29 23:02:13.291: I/dfasdf(7039): IN CURSOR TO FOOENRTY  3-3   id: 3
09-29 23:02:13.311: I/dfasdf(7039): IN CURSOR TO FOOENRTY  1-0   id: 5

编辑:当然一个解决方案是只获取最新游戏的ID,然后单独获取它们,但我的问题的动机更多是要了解为什么会发生这种情况。< / p>

1 个答案:

答案 0 :(得分:2)

向后移动光标似乎不起作用。

但是,如果您没有明确指定排序,则无法保证以任何特定顺序返回记录,因此修改查询以便它完全返回您想要的记录将更容易:

cursor = database.query(CustomSQLiteHelper.TABLE_FOOTBALL,
                        allColumns2,
                        null, null,
                        null, null,
                        "id DESC",  // return largest id values first
                        "3");       // return no more than 3 records
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
    ...
    cursor.moveToNext();
}