【发布时间】:2013-03-06 23:59:08
【问题描述】:
以下语句cursor.moveToNext() 总是错误的。我希望循环执行一次。我已经测试了查询实际上返回了数据。
有谁知道这是怎么回事?
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);
}
【问题讨论】:
-
是否与查询语句中不使用“FROM”导致SQLite无法识别为有效命令有关?
-
nop ...您的查询应该只返回一行,因此当您使用 cursor.moveToFirst() 时,您位于第一行...现在任何调用 moveToNext() 都将返回 false ... mCursor 应该永远不要为空,所以使用像
if(mCursor.moveToFirst()){do{/*String res... your stuff*/}while(mCursor.moveToNext());}else{/*cursor has no rows should not happend*/}这样的 smth 或者在这种情况下(因为它应该只返回 1 行if(mCursor.moveToFirst()){/*String res... your stuff*/}else{/*cursor has no rows WTF?*/} -
我爱你宝贝。问题解决了!!!去掉 cursor.moveToFirst() 之后……哈哈