目前,我在使用 sqlite 数据库的 android 中遇到了很多麻烦。这一次,真的很奇怪。
每次我进入某个活动时,我都想显示最近的三个条目,例如“团队 1 与团队 2 目标 - 目标”。奇怪的是,数据库返回了错误的值。让我们从当前的数据库内容开始:
对我的问题很重要的是列“id”、“goals1”和“goals2”。现在如您所见,最近的三场比赛是“4: dfsadf vs. asf 3-2”、“3: df vs. as 3-2”和“2: dfa vs. dfas 2-0”。在我的活动中,显示以下内容:
如您所见,客队目标的目标是错误的(并且条目的 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);
现在显示
这是我的 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)。获得单个游戏总是会返回正确的值。如果我得到所有游戏(需要在不同的活动中),也会发生同样的事情。在数据库创建声明中,只有一个值设置为“autoincrement”,并且是_id,没有别的。
现在为了“证明”这一点,我输入了一个新条目,得分 1-0:以下是显示的(设置文本的第二个变体,意味着第一个是正确的)
日志
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,并分别获取它们,但我的问题的动机更多是为了理解为什么会发生这种情况。