2014-05-07 167 views
-2

我不斷收到在logcat中以下異常:無法查詢的Android SQLite數據庫

>05-07 11:26:19.276: E/AndroidRuntime(1608): FATAL EXCEPTION: main 
05-07 11:26:19.276: E/AndroidRuntime(1608): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.db_003/com.db_003.MainActivityDb3}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
05-07 11:26:19.276: E/AndroidRuntime(1608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
05-07 11:26:19.276: E/AndroidRuntime(1608): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.os.Looper.loop(Looper.java:137) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at dalvik.system.NativeStart.main(Native Method) 
05-07 11:26:19.276: E/AndroidRuntime(1608): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.database.CursorWindow.nativeGetLong(Native Method) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.database.CursorWindow.getLong(CursorWindow.java:507) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.database.CursorWindow.getInt(CursorWindow.java:574) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at com.db_003.DB_Helper.cursorToList(DB_Helper.java:88) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at com.db_003.DB_Helper.queryCustom(DB_Helper.java:105) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at com.db_003.MainActivityDb3.onCreate(MainActivityDb3.java:29) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.app.Activity.performCreate(Activity.java:5008) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
05-07 11:26:19.276: E/AndroidRuntime(1608):  ... 11 more> 

這裏是我的查詢代碼:

Log.d("Select: ", "All Kerry towns"); 
    String table = DB_Helper.TABLE_TOWNS; 
    String where = " WHERE " + DB_Helper.COUNTY + " = 'Kerry'"; 
    String strSQL = "SELECT " + DB_Helper.COUNTY + " FROM " + table + where + ";"; 
    Log.d("Select SQL Statement: ", strSQL); 
    List<Town> towns = db.queryCustom(strSQL); 
    for (Town t : towns) { 
     Log.d("Kerry: ", t.toString()); 
    } 

這些數據庫的信息字段:

public static final String DATABASE_NAME = "towns"; 
public static final int DATABASE_VERSION = 2; 
public static String _ID = "_id"; 
public static final String TABLE_TOWNS = "towns"; 

public static final String TOWN = "town"; 
static final String COUNTY = "county"; 
public static final String PROVINCE = "province"; 
public static final String COUNTRY = "country"; 

以下是DB_Helper.java中的查詢方法:

public List<Town> queryCustom(String strSQL) { 
    SQLiteDatabase db = getReadableDatabase(); 
    Cursor cursor = db.rawQuery(strSQL, null); 
    List<Town> townList = cursorToList(cursor); 
    db.close(); 
    return townList;   
} 

而且cursorToList方法:

private List<Town> cursorToList(Cursor cursor) { 
    List<Town> townList = new ArrayList<Town>(); 
    if (cursor.moveToFirst()) { 
     do { 
      // get the field details from the cursor     
      int id = cursor.getInt(cursor.getColumnIndex(_ID)); 
      String town = cursor.getString(cursor.getColumnIndex(TOWN)); 
      String county = cursor.getString(cursor.getColumnIndex(COUNTY)); 
      String province = cursor.getString(cursor.getColumnIndex(PROVINCE)); 
      String country = cursor.getString(cursor.getColumnIndex(COUNTRY)); 
      // create a new empty instance of 
      // Country and add it to the list 
      townList.add(new Town(id, town, county, province, country)); 
     } while (cursor.moveToNext()); 
    } 
    return townList; 
} 

此選擇所有列時工作正常,但我無法選擇特定的列。將不勝感激的幫助。

+0

'cursor.getColumnIndex(_ID)'返回-1 ...猜測爲什麼(或閱讀文檔)... – Selvin

回答

2

你的cursorToList()假設所有5列在Cursor。當您只查詢一列時,您會得到-1作爲缺失列的列索引,並使用該索引get...()可以爲您提供例外。

要麼始終查詢所有列,要麼在嘗試使用它之前檢查索引是否不是-1。

+0

謝謝,似乎這樣的伎倆。將cursorToList方法更改爲僅獲取TOWN列。 – jchoran

-1

顯然-1正在用這種方法調用

cursor.getColumnIndex(COUNTY) 

你有沒有在表中的任何數據恢復?也許有一個拼寫錯誤, 「縣」和「國家」可以很容易地混合起來

+0

'清楚-1正在通過這種方法返回 cursor.getColumnIndex(COUNTY)'不,它不是...有一個縣列在選擇查詢...有沒有id_和其他... – Selvin

1

這樣做希望這對你有效。

public List<Town> cursorToList(Cursor cursor) { 
     List<Town> townList = new ArrayList<Town>(); 
     if (cursor.moveToFirst()) { 
      do { 
       HashMap<String, String> col = new HashMap<String, String>(); 
       int size = cursor.getColumnCount(); 
       for (int i = 0; i < size; i++) { 
        col.put(cursor.getColumnName(i), cursor.getString(i)); 
        townList.add(new Town(col.get(_ID), col.get(TOWN), col.get(COUNTY), col.get(PROVINCE), col.get(COUNTRY))); 
       } 
      } while (cursor.moveToNext()); 
     } 
     if (cursor != null && !cursor.isClosed()) { 
      cursor.close(); 
     } 
     return townList; 
    } 
+0

謝謝,這也工作。 – jchoran