无法查询Android SQLite数据库

时间:2014-05-07 11:47:07

标签: android sqlite

我在LogCat中一直收到以下异常:

>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;
}

这在选择所有列时工作正常,但我无法选择特定列。非常感谢帮助。

3 个答案:

答案 0 :(得分:2)

您的cursorToList()假设所有5列都在Cursor中。当您只查询一列时,您将获得-1作为缺失列的列索引,并将该索引与get...()一起使用将为您提供异常。

要么总是查询所有列,要么在尝试使用它之前检查索引是否为-1。

答案 1 :(得分: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;
    }

答案 2 :(得分:-1)

显然,此方法调用返回-1

cursor.getColumnIndex( COUNTY ) 

表格中有数据吗?也许在某处有拼写错误, “县”和“国家”可以很容易地混在一起