【问题标题】:CursorIndexOutOfBoundException: Index -1 requested, with a size of 1CursorIndexOutOfBoundException:请求索引 -1,大小为 1
【发布时间】:2018-07-17 08:10:24
【问题描述】:

我如何执行检索 2 个字符串和 1 个整数值的选择语句。我收到此错误。我的应用程序一直在 cursor.getString(0);在我的日志中,我确实收到了这条消息 Log.d("TAG","Row found");

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

【问题讨论】:

  • 我已经评论 cursor.getString(0) 来检查我的日志

标签: android sql sqlite android-sqlite


【解决方案1】:

你只需要获取列的索引

int index = cursor.getColumnIndex(COLUMN_NAME);

然后

String columnValue = cursor.getString(index);

祝你好运:)

【讨论】:

  • int index_pin = cursor.getColumnIndex("pin"); int index_url = cursor.getColumnIndex("url"); int index_auth = cursor.getColumnIndex("auth"); String columnValue_pin = cursor.getString(index_pin);字符串 columnValue_url = cursor.getString(index_url); String columnValue_auth = cursor.getString(index_auth);
  • 如果您有“url”、“auth”或“pin”之类的列,是的。
猜你喜欢
  • 1970-01-01
  • 2022-01-12
  • 2018-11-04
  • 2014-05-06
  • 2017-12-24
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多