2012-12-22 21 views
0

我的光標似乎並沒有工作。有人能幫幫我嗎? 其實for循環在這裏不起作用。日誌沒有顯示。 這是我的代碼:光標似乎沒有在Android應用程序

public String getAFact(int rowNumber) 
    { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor c = db.rawQuery("select mfacts from mfacts where osl_number=" + rowNumber + ";", null); 
     for(c.moveToFirst();!c.isAfterLast();c.moveToNext()) { 
      rowData = c.getString(c.getColumnIndex(KEY_NAME)); 
      Log.i("log_tag", "cursor isn't f**ked up..."+rowData); 
     } 
     c.close(); 
     db.close(); 
     return rowData; 
    } 
} 

但無論如何,下面的代碼工作正常,並顯示正確的記錄數!

public int countRowsInDb() 
    { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor c = db.rawQuery("select * from mfacts",null); 
     Log.i("Number of Records"," :: "+c.getCount()); 
     db.close(); 
     int c_getCount = c.getCount(); 
     c.close(); 
     return c_getCount; 
    } 
+0

是否表有一行其中' 「osl_number」''等於rowNumber'? – Sam

+0

是的。但是,無論如何我可以檢查它嗎?只是爲了確定它? – defiant

+0

在Eclipse中,您可以使用文件資源管理器窗口將數據庫「拉」到硬盤上,然後手動檢查它。但是如果沒有看到提及「index -1 ...」的錯誤,那麼該行可能不存在。 – Sam

回答

1

我認爲你的SQL語句已關閉。它看起來像你試圖獲取數據的整行,儘量

"select * from mfacts where osl_number=" + rowNumber + ";"

爲您的查詢。

+0

試過了,但還是沒有運氣! :/ – defiant

0

U可以試試這個..

if (cursor.moveToFirst()) 
      {      
       for (int i = 0; i < cursor.getCount(); i++) 
       { 
        //Your logic goes here// 
        cursor.moveToNext(); 
//     
       }   
      } 
      cursor.close(); 
      db.close(); 
+0

但這就是我在for循環中所做的事情嗎?我沒有看到任何區別! – defiant

+0

我在我的項目中使用這個邏輯..它完美的工作..然後你必須檢查其他..是否有數據庫中的數據?是logcat顯示任何錯誤? – Subburaj

相關問題