我不斷收到在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;
}
此選擇所有列時工作正常,但我無法選擇特定的列。將不勝感激的幫助。
'cursor.getColumnIndex(_ID)'返回-1 ...猜測爲什麼(或閱讀文檔)... – Selvin