2010-08-27 86 views
0

我正在嘗試循環創建制表符的數據庫值。我設置了一個名爲createTab的方法,它接受一個Long值和一個String值。它正在處理靜態數據,但我很努力去理解如何遍歷SQLite數據庫記錄。如何在for循環或while循環中引用遊標項?

這裏是我的失敗嘗試(小於符號替換[每種不超過]):


for (int i = 0; i [lessthan] mCursor.getCount(); i++) 
{ 
createTab(
    mCursor.getLong(mCursor.getColumnIndexOrThrow("_id")), 
    mCursor.getString(mCursor.getColumnIndexOrThrow("category"))); 
} 

你可能不需要的logcat知道我做錯了什麼在上面的代碼,但以防萬一...

 
08-27 21:28:18.268: ERROR/AndroidRuntime(232): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3 
08-27 21:28:18.268: ERROR/AndroidRuntime(232):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
08-27 21:28:18.268: ERROR/AndroidRuntime(232):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172) 
08-27 21:28:18.268: ERROR/AndroidRuntime(232):  at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:99) 
08-27 21:28:18.268: ERROR/AndroidRuntime(232):  at com.toBuy.Main.createTabs(Main.java:51) 
08-27 21:28:18.268: ERROR/AndroidRuntime(232):  at com.toBuy.Main.onCreate(Main.java:38) 
08-27 21:28:18.268: ERROR/AndroidRuntime(232):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
08-27 21:28:18.268: ERROR/AndroidRuntime(232):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 
08-27 21:28:18.268: ERROR/AndroidRuntime(232):  ... 11 more 
08-27 21:28:18.278: INFO/Process(54): Sending signal. PID: 232 SIG: 3 
08-27 21:28:18.278: INFO/dalvikvm(232): threadid=7: reacting to signal 3 
08-27 21:28:18.338: INFO/dalvikvm(232): Wrote stack trace to '/data/anr/traces.txt' 
08-27 21:28:18.508: INFO/ARMAssembler(54): generated scanline__00000077:03515104_00000000_00000000 [ 27 ipp] (41 ins) at [0x285a68:0x285b0c] in 713498 ns 
08-27 21:28:18.518: INFO/ARMAssembler(54): generated scanline__00000077:03515104_00001001_00000000 [ 64 ipp] (84 ins) at [0x285b10:0x285c60] in 1897448 ns 
08-27 21:28:28.086: WARN/ActivityManager(54): Launch timeout has expired, giving up wake lock! 
08-27 21:28:28.097: WARN/ActivityManager(54): Activity idle timeout for HistoryRecord{4390bf70 com.toBuy/.Main} 

謝謝你的幫助。

回答

0

您需要定位光標,然後才能呼叫getLonggetString。在發出查詢之後,Cursor就位於第一行之前,因此您應該在循環中調用moveToNext。喜歡的東西:

int size = mCursor.getCount(); 
for (int i = 0; i < size; i++) { 

    // Position the cursor 
    mCursor.moveToNext(); 

    // Fetch your data values 
    long id = mCursor.getLong(mCursor.getColumnIndex("_id")); 
    String cat = mCursor.getString(mCursor.getColumnIndex("category")); 
} 
+0

謝謝你的迴應。我在這裏掙扎的是引用遊標中的列。 //如何獲取數據值查找長列(_id)和字符串列(類別)? – alockrem 2010-08-27 22:18:57

+0

您仍然可以像訪問樣本一樣訪問列值。 Cursor對象包含一個指向數據庫中當前指向的行的指針,而您的示例代碼只需在調用任何get *方法之前對其進行定位即可。如果這樣不能解決您的問題,我會將數據庫從設備(或模擬器)中取出,並仔細檢查模式。 – 2010-08-28 01:01:26

0

使用for循環如下:

for(mCursor.moveToFirst();!mCursor.isAfterLast();mCursor.moveToNext()){ 
     createTab(
    mCursor.getLong(mCursor.getColumnIndexOrThrow("_id")), 
    mCursor.getString(mCursor.getColumnIndexOrThrow("category")));  
     } 

雖然,while循環就會簡單得多:

while (mCursor.moveToNext()) { createTab(
    mCursor.getLong(mCursor.getColumnIndexOrThrow("_id")), 
    mCursor.getString(mCursor.getColumnIndexOrThrow("category")));  

}

在這兩種情況下,你不需要手動設置光標的起始位置。

1

在我的代碼中,我從SQLite數據庫中獲取值,並使用Cursor將該值存儲在模型類(FriendData是我的模型類)的對象數組中。

Cursor cursor = friendAdapter.fetchData(); //call method for fetch data from databse and fetched data is stored into cursor 
    int cursorSize = cursor.getCount(); 

    if (cursor != null && cursor.moveToFirst()) { 
     FriendData[] friendData=new FriendData[cursorSize]; //create array of objects as size of cursor 

      for (int i = 0; i < cursorSize; i++) { 
        friendData[i]=new FriendData(); 

        /*store fetched data in some variables. In my code I have store value of cursor in friendName String variable*/ 
        friendName = cursor.getString(cursor.getColumnIndex(FriendAdapter.FRIEND_NM)); 

       /* set friendName variable value in array of object*/ 
       friendData[i].setFriendName(friendName); 

       cursor.moveToNext(); //move cursor for pointing to next fetched record 
      } 
    } 

首先我有檢查光標條件是否爲空,並移動光標對象指向第一個記錄被提取。然後我使用方法getCount()計算遊標的大小。之後for循環用於存儲對象數組中的所有數據,並在結束時光標指向下一個被提取的記錄。