光标索引超出界限异常

时间:2017-06-08 12:49:42

标签: java android sqlite android-sqlite

我不断收到此错误消息:"由以下引起:android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0"在我的代码中。

我已经引用了以下链接,但它们都没有工作: Android cursor out of bounds exception

Cursor index out of bounds

Cursor Index Out Of Bounds Error Android?

cursor index out of bounds exception

  public Product getProduct(int id) {
    db = this.getReadableDatabase();

    Cursor cursor = db.query(
            TABLE_NAME,
            null,
            COLUMN_ID + " = ?",
            new String[] { Integer.toString(id)},
            null, null, null, null);

    Product product = new Product();

    cursor.moveToFirst();
    if (cursor != null) {
        product.setID(cursor.getInt(0));
        product.setName(cursor.getString(1));
        product.setSize(cursor.getDouble(2));
        product.setHp(cursor.getDouble(3));
        product.setCategory(cursor.getString(4));
        product.setPowerType(cursor.getString(5));
        product.setExplosionProof(cursor.getInt(6));
        product.setRPM(cursor.getInt(7));
        product.setBypassPSI(cursor.getInt(8));
        product.setGPM(cursor.getInt(9));
        product.setPrice(cursor.getDouble(10));
        cursor.close();
    }

    db.close();
    return product;
}

以下是我的常数:

private static final String TABLE_NAME = "Products";
private static final String COLUMN_ID = "'_id'";

我非常感谢任何帮助。

现在,它不会进入if循环。我直接从另一个堆栈溢出问题得到了该查询模型。

1 个答案:

答案 0 :(得分:0)

光标很可能是空的,因此您需要确定原因。可能是没有底层数据,或者可能是cursor where子句排除了所有数据。根据你提供的内容,无法确定原因。

我建议删除where子句以查看此更改是否重要。

即。而不是

    Cursor cursor = db.query(
        TABLE_NAME,
        null,
        COLUMN_ID + " = ?",
        new String[] { Integer.toString(id)},
        null, null, null, null);

使用: -

    Cursor cursor = db.query(
        TABLE_NAME,
        null,
        null,
        null,
        null, null, null, null);

这将返回游标中的所有行,如果这样做,则问题在于where子句(传递给db.query的第3和第4个参数)。如果它没有解决问题,那么表本身可能没有行。

您可以使用: -

来规避CursorIndexOutOfBoundsException
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        product.setID(cursor.getInt(0));
        product.setName(cursor.getString(1));
        product.setSize(cursor.getDouble(2));
        product.setHp(cursor.getDouble(3));
        product.setCategory(cursor.getString(4));
        product.setPowerType(cursor.getString(5));
        product.setExplosionProof(cursor.getInt(6));
        product.setRPM(cursor.getInt(7));
        product.setBypassPSI(cursor.getInt(8));
        product.setGPM(cursor.getInt(9));
        product.setPrice(cursor.getDouble(10));
    } else {
        // setup to return a product not found
    }
    cursor.close();

但是,这不会解决游标没有返回任何行的基本问题。

您可以使用以下内容来获取有关光标的更多信息(使用光标按照上面的更改选择所有行来运行)。这适用于任何游标: -

    int rowcount = cursor.getCount();
    String colnames = "";
    for (String columns : cursor.getColumnNames()) {
        colnames = colnames +
                "Column Index=" +
                Integer.toString(cursor.getColumnIndex(columns)) +
                " Name=>" +
                columns +
                "<  ";
    }
    Log.d("DBCHK","Table has " + Integer.toString(rowcount) + " rows. With columns:- " + colnames);
    String values;
    while (cursor.moveToNext()) {
        values = "";
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            values = values +
                    "Column = " +
                    cursor.getColumnName(i) +
                    " Index=" +
                    Integer.toString(i) +
                    " Value=" + cursor.getString(i) + ": ";
        }
        Log.d("DBCHK",values);
    }

这会将数据写入日志: -

D/DBCHK: Table has 2 rows. With columns:- Column Index=0 Name=>_id<  Column Index=1 Name=>myfloat<  
D/DBCHK: Column = _id Index=0 Value=null: Column = myfloat Index=1 Value=56.7897: 
D/DBCHK: Column = _id Index=0 Value=null: Column = myfloat Index=1 Value=87.7655: 

每行会有一行,前面有一行列出了行数和列数。

相关问题