0

我正在尝试从我的数据库中选择某些行,但是由于我一直使索引超出范围,因此我决定暂时删除该WHERE部分并获取所有数据。我使用了 android 教程http://developer.android.com/training/basics/data-storage/databases.html#DbHelper来创建这个方法。但是当我试图从查询中获取值时,我得到了 CursorIndexOutOfBoundsException。

我的代码:

String getSite() {
    SQLiteDatabase db = this.getReadableDatabase();
            //get all data for now
    Cursor c = db.query(
        TABLE_NAME,  // The table to query
        null,                               // The columns to return
        null,                                // The columns for the WHERE clause
        null,                            // The values for the WHERE clause
        null,                                     // don't group the rows
        null,                                     // don't filter by row groups
        null                                 // don't sort
        );
    c.moveToFirst();
        String test= c.getString(0); 
    c.close();
    return test;}
}

错误: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 6

当我检查 c.count() 时,我得到了 6,所以我不确定为什么我指的是可能超出范围的索引

谢谢

4

1 回答 1

0

如果光标没有移动,则它是空的。

尝试

if (c.moveToFirst()) {
    String test= c.getString(0);
    // or String test= c.getString(c.getColumnIndex("column_name")); 
}
于 2013-01-21T20:31:12.880 回答