2012-09-29 55 views
1

目前我在android和sqlite数据库中遇到了很多麻烦。这一次,这真的很奇怪。android sqlite:获取错误值

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

enter image description here

在这里,我的问题是很重要的列“ID”,“目标1”和“目标2”。现在你看,最近的三场比赛是“4:dfsadf vs. asf 3-2”,“3:df vs. as 3-2”和“2:dfa vs. dfas 2-0”。在我的活动,以下是displaid:

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); 

现在displaid

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场比赛都没有区别,只要他们是相同的比赛,显然必须是(看到身份证的)。获得单一游戏总是会返回正确的值。同样的事情发生,如果我得到所有的游戏(需要在不同的活动)。在数据库创建声明中,只有一个值设置为“autoincrement”,并且只有_id,没有别的。

我们“证明”这个,我进入了一个新的项目,比分1-0:以下是displaid(与设置文本的第二个变体,是指第一个将是正确的)

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的,并且让他们seperately,但我的问题的动机是更加理解为什么这样可以发生。

+0

你能发表您的查询吗? –

+0

由于我对数据库非常陌生,哪个查询完全是你的意思?现在我明白了,通过使用SQLiteOpenHelper的扩展和查询,我将光标放在特定的位置(然后我“手动”移动它,如“moveToNext()as long xy”)。 –

+0

你可以发布查询,比如当你做SELECT或类似的部分(你使用哪个,contentresolver或wat) – lemoncodes

回答

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(); 
} 
+0

我得到一个java.lang.IllegalArgumentException:无效的LIMIT子句:限制3 –

+0

编辑; 'LIMIT'由'query()'自动插入。 –

+0

非常感谢! –