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