在执行SQLiteDatabasequey类的quey()方法时,返回的是一个Cursor
开始我以为只要数据库中查询不到的满足要求的数据时,返回的CUrsor就是null,但是在实际调整代码时,发现就算是查询到的结果是空的时候,Cursor的值都算不是null。于是很纳闷了,那Cursor在什么情况下的值是Null? 在Android的samples中的Notepad中有这样一段代码
public Cursor fetchNote(long rowId) throws SQLException {         Cursor mCursor =             mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                     KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
                     null, null, null, null);
         if (mCursor != null) {
             mCursor.moveToFirst();
         }
         return mCursor;
     }按照我上面的理解,既然query()返回的Cursor肯定是不会为Null,那么上面代码 中的
if(mCursor != null )
这一步的判断不是没有任何意义吗?求解释。
还有一个问题是如何根据Cursor的情况来判断查询结果是否为空?
小弟现在采用的方法时判断Cursor.getCount() == 0 来判断查询就过是不是为空,请问还有什么其他比较简单的方法吗? 

解决方案 »

  1.   

    我用这个。。
    Cursor.moveToFirst() == false
    moveToFirstboolean moveToFirst()
    Move the cursor to the first row.
    This method will return false if the cursor is empty.Returns:
    whether the move succeeded.
      

  2.   

    lz好好看看文档!
    http://developer.android.com/reference/android/database/Cursor.html#moveToFirst()Move the cursor to the first row.
    This method will return false if the cursor is empty.
    Returnswhether the move succeeded.
    我就郁闷了。。连google自己写的都不相信?!!