android sqlite:获取错误的值
目前,我在android中使用sqlite数据库遇到了很多麻烦。这次真的很奇怪 每次我输入某个活动时,我都希望显示最近的三个条目,如“团队1与团队2目标-目标”。奇怪的是,数据库返回了错误的值。让我们从当前数据库内容开始: 对于我的问题,这里有“id”、“goals1”和“goals2”列。现在正如你所看到的,最近的三场比赛是“4:dfsadf对ASF3-2”,“3:df对as 3-2”和“2:dfa对dfas 2-0”。在我的活动中,以下内容令人不快: 如您所见,客场球队目标的目标是错误的(而且似乎条目的ID设置为客场目标)。 我将这三个按钮的文本设置为:android sqlite:获取错误的值,android,sqlite,Android,Sqlite,目前,我在android中使用sqlite数据库遇到了很多麻烦。这次真的很奇怪 每次我输入某个活动时,我都希望显示最近的三个条目,如“团队1与团队2目标-目标”。奇怪的是,数据库返回了错误的值。让我们从当前数据库内容开始: 对于我的问题,这里有“id”、“goals1”和“goals2”列。现在正如你所看到的,最近的三场比赛是“4:dfsadf对ASF3-2”,“3:df对as 3-2”和“2:dfa对dfas 2-0”。在我的活动中,以下内容令人不快: 如您所见,客场球队目标的目标是错误的
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;
}
现在不高兴了
这是我的入门方法
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:下面是displaid(第二个变量设置文本,意味着第一个将是正确的)
日志
编辑:当然,解决方案是只获取最新游戏的ID,然后分别获取它们,但我的问题的动机更多的是理解为什么会发生这种情况。向后移动光标似乎不起作用 但是,如果不显式指定顺序,则无法保证以任何特定顺序返回记录,因此修改查询以使其准确返回所需的记录将更容易:
你能发布你的查询吗?因为我对数据库很陌生,你到底指的是哪个查询?据我现在所知,通过使用SQLiteOpenHelper的扩展,使用querys,我将光标放在一个特定的位置(然后我“手动”移动它,比如“moveToNext(),长xy”)。您可以发布查询吗,比如执行SELECT或类似操作时的部分(无论您使用的是contentresolver还是wat)我得到一个java.lang.IllegalArgumentException:无效的限制子句:限制3已编辑;
限制
由query()
自动插入。如何在上面指定跳过syntax@kishore一点也不;使用rawQuery
@CL他们不需要原始查询,我指定了3,2,它在3是跳过值,2是限制值的情况下工作。
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
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
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();
}