0

嘿,我正在使用游标查询数据库,但没有指向任何行,即在调试时显示 mrowId = null ,count =70(我使用内容值插入的内容在那里)但是当我写 cursor.moveToNext () 或 cursor.moveToFirst(); 它会抛出光标索引超出界限异常,而 cursor.moveToNext() 或 cursor.moveToFirst() 总是在 eclipse 中的 watch 表达式中返回......我希望你理解这个问题帮助我解决这个问题...... ..感谢您
为简单起见提前提出的建议我正在粘贴代码

public Cursor getDataBaseCursor(String tableName) {
    Cursor cursor = null;
    try {
        if (mainDatabase.isOpen()) {
            String countQuery = "SELECT  * FROM " + tableName;
            cursor = mainDatabase.rawQuery(countQuery, null);
            // cursor = mainDatabase.query(tableName, null, null, null,
            // null, null, null);
            if (cursor != null) {
                **boolean check =cursor.moveToNext();**
                return cursor;
            }

        } else {
            createOrOpenDatabase(databaseName);
            cursor = mainDatabase.query(tableName, null, null, null, null, null, null);
            if (cursor != null) {
                cursor.moveToFirst();
                return cursor;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        cursor = mainDatabase.query(tableName, null, null, null, null, null, null);
        if (cursor != null) {
            cursor.moveToNext();
            return cursor;
        }

    }
    return null;

}

调试时布尔检查为真

4

2 回答 2

0

可能这段代码会帮助你,

public Cursor getDataBaseCursor(String tableName) {
    Cursor cursor = null;
    try {
        if (mainDatabase.isOpen()) {
            String countQuery = "SELECT  * FROM " + tableName;
            cursor = mainDatabase.rawQuery(countQuery, null);

            int fieldValue; 
            if(cursor.getCount()>0){
                 cursor.moveToFirst();
                 do
             {
                    fieldValue=cursor.getInt(cursor.getColumnIndex("tbl_FieldName"));   
                    System.out.println("The Value of fatched field :"+fieldValue);
                 }while(cursor.moveToNext());
        cursor.close();
            }

          System.out.println("The Value of field :"+fieldValue);
       }
       catch (Exception e)
       {
     e.toString();
       }
于 2012-05-10T11:17:34.093 回答
0

代替

if (cursor != null) {
**boolean check =cursor.moveToNext();**
return cursor;

尝试

Log.d(TAG, "cursor.getCount()= " + String.valuseOf(cursor.getCount))
if(cursor.getCount > 0) {
    cursor.moveToFirst()
}
于 2012-05-10T11:26:06.987 回答