Android:SQLite,cursor.moveToNext()始终返回false

以下语句cursor.moveToNext()始终为false.我希望循环执行一次.我已经测试过查询实际返回的数据.

有谁知道是什么事吗?

String query ="SELECT(SELECT COUNT(*) FROM Table1) as count1,(SELECT COUNT(*) FROM Table2) as count2;";

    Cursor mCursor = mDb.rawQuery(query,null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }

    while (cursor.moveToNext()) {   //<---------------return false here???
        String result_0=cursor.getString(0);
    }

解决方法

我知道你已经解决了你的问题,但这是对发生的事情的一个演练:
Cursor mCursor = mDb.rawQuery(query,null);

// At this point mCursor is positioned at just before the first record.

if (mCursor != null) {
    mCursor.moveToFirst();
    // mCursor is now pointing at the first (and only) record
}

while (mCursor.moveToNext()) {
    String result_0=cursor.getString(0);
}

// The loop above was skipped because `.moveToNext()` caused mCursor
// to move past the last record.

因此,在您只需要一条记录的情况下,您只需要mCursor.moveToFirst()或您的mCursor.moveToNext().

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...