2012-09-29 64 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

非常感謝! –