CursorIndexOutOfBoundException:请求的索引-1,大小为1

时间:2018-07-17 08:10:25

标签: android sql sqlite android-sqlite

我该如何执行select语句来检索2个String和1个整数。我收到此错误。我的应用程序不断在cursor.getString(0)崩溃;在我的日志中,确实收到此消息Log.d(“ TAG”,“找到行”);

Login.java

private void getQRCodeInformation(){
    //database helper object
    DatabaseHelper db;
    //initializing views and objects
    db = new DatabaseHelper(this);
    Cursor cursor = db.getQRCodeInformation();
    if(cursor.getCount() == 0) {
        Log.d("TAG","Nothing found");
    }
    else{
        Log.d("TAG","Row found");
        if(cursor != null && cursor.moveToFirst()){
            //cursor.getString(0);
            //cursor.getString(1);
            //cursor.getInt(1);
            Log.d("TAG","Data found");
        }
    }
}

DatabaseHelper.java

public Cursor getQRCodeInformation(){
    SQLiteDatabase db = this.getReadableDatabase();
    String sql = "SELECT "+COLUMN_0_UserDetail+" , "+COLUMN_4_UserDetail+" , "+COLUMN_5_UserDetail+ " FROM "+ TABLE_NAME_UserDetail;
    Cursor c = db.rawQuery(sql, null);
    return c;
}

1 个答案:

答案 0 :(得分:0)

您只需要获取列的索引

  

int索引= cursor.getColumnIndex(COLUMN_NAME);

然后

  

字符串columnValue = cursor.getString(index);

祝你好运:)