Android:SQLite,cursor.moveToNext() 总是返回 false

标签 android sql sqlite

以下语句 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:SQLite,cursor.moveToNext() 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15260598/

相关文章:

android - 为什么我不能从/dev/graphics/fb0 读取数据?

android - 如何使用 VPNService 阻止应用程序网络请求?

SQL 查询 - 我哪里出错了?

php - 如何使用PHP编写MySQL搜索查询

java - 如何在 SQLite 中解析 MySQL 时间戳

android - 如何在安卓 Activity 中显示虚拟键盘

java - 使用 AppCompat PreferenceActivity 为单个应用程序提供多个首选项?

sql - PostgreSQL ORDER BY 问题 - 自然排序

ios - 无法从 iOS 应用程序打开 sqlite 数据库

sql - sqlite 中简单字符串表的奇怪行为