SQLite Cursor row ID NULL - 经验丰富的 Android 开发人员


嘿,我正在将

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

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

调试时布尔检查为 true

也许这段代码会对你有所帮助,

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

而不是

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

尝试

Log.d(TAG, "cursor.getCount()= " + String.valuseOf(cursor.getCount))
if(cursor.getCount > 0) {
    cursor.moveToFirst()
}

相关内容

  • 没有找到相关文章