2013-04-26 73 views
0

基本上我有這樣的:光標#的getType()是指數越界

Cursor cur = ... 
for (int i = 0; i < cur.getColumnCount(); i++) { 
    String name = cur.getColumnName(i); 
    Log.d("dao",name); 
    int type = cur.getType(i); 

...並在getType()通話以上時出現例外。 列名稱記錄正確。

錯誤AndroidRuntime產生的原因: android.database.CursorIndexOutOfBoundsException:指數-1要求, 大小爲0

+0

可能重複的[cursor.getType()和CursorIndexOutOfBoundsException異常](http://stackoverflow.com/questions/8133784/cursor-gettype-and-cursorindexoutofboundsexception-exception) – 2013-04-27 22:22:27

回答

1

這是SQLite數據庫,使用動態類型的特徵。請檢查this。 你爲什麼不使用:

Cursor cur = ... 

if (cursor != null) { 
    while (cursor.moveToNext()) { 
     String name = cur.getColumnName(i); 
     int type = cur.getType(i); 
     ... 
    } 
} 

這樣,你只要求類型,如果行存在。

如果你需要列獨立輸入任何結果,您可以使用:

String tableName = "..."; 
Cursor cursor = rawQuery("pragma table_info(" +tableName +")"); 
String type = getString(0); 
String name = getString(1); 

希望這有助於...乾杯!

+0

我真的做了測試,但我沒有' t發佈它。我的Cursor測試中有一個錯誤。無論如何,謝謝你的解釋。 – PeterMmm 2013-04-27 06:38:09