2016-08-24 35 views
-1

我SQLite中的Android查詢:光標查詢再回到1 mCount

Cursor cursor = Application.getInstance().getContentResolver().query(DAOProvider.URI_EXECSQL, new String[]{getSingleQuery(idPatient)}, null, null, null);` 
    if (cursor != null && cursor.moveToFirst()) { 
     if(cursor.getCount() > 0) 
     { 
     String id = cursor.getString(0); // The result of all the values are null 
     } 
    } 

這是查詢的結果(所有值都爲空,該表是空的):

enter image description here

問題是,返回count = 1,但我沒有任何記錄在我的數據庫中,我的表是空的,並且遊標認爲存在1記錄,但問題是遊標具有NULL中的所有值。

是否需要任何其他配置來檢查查詢是否有結果? 添加轉儲光標:

Dumping cursor [email protected] 
0 { 
    id_person_patient=null 
    id_blood_type=null 
    cenRegister=null 
    name=null 
    paternalSurname=null 
    maternalSurname=null 
    sex=null 
    curp=null 
    rfc=null 
    registerDate=null 
    birthday=null 
    email=null 
    id_nationality=null 
    id_state=null 
    IFNULL(nfcp.id_statusnfc, 0)=0 
    COUNT(pr.id)=0 
    id_person_tutor=null 
    id_relationship=null 
    name=null 
    paternalSurname=null 
    maternalSurname=null 
    sex=null 
    curp=null 
    rfc=null 
    registerDate=null 
    birthday=null 
    email=null 
    id_nationality=null 
    id_state=null 
    IFNULL(vc.apply, 0)=0 
    id_person_user_alta=null 
    nfccount=0 
} 
+0

如果不能確定moveToPosition()不能返回正確的'Cursor'內容使用'DatabaseUtils#dumpCursor'方法 – pskink

+0

'「」「這是查詢的結果(所有的值都爲空,表爲空):」「」',不,表不是空的,你有一行(所有字段或者= 0或NULL) – pskink

+0

問題是IFNULL(nfcp.id_statusnfc,0)= 0,這使得查詢在結果行中返回0值。 –

回答

1

看起來像你對我有一個行與空值id_person_patientid_blood_type。如果表格爲空,則moveToFirst()將返回錯誤,顯然計數不會是> 0

如果你仍然不想相信,請考慮: SqliteCursor延伸AbstractWindowedCursor延伸AbstractCursor

AbstractCursor.moveToFirst()來電moveToPosition(0)。這裏的moveToPosition()

public final boolean moveToPosition(int position) { 
    // Make sure position isn't past the end of the cursor 
    final int count = getCount(); 
    if (position >= count) { 
     mPos = count; 
     return false; 
    } 
    ... 

您的位置通過0,所以它必須返回false,如果計數爲0。總之,如果getCount將()返回0

+0

但是,如果結果是成立的,結果是一樣的,計數將是1,我的查詢只返回一個結果。 –

+0

我們如何知道計數不是一個?請自己閱讀:https://developer.android.com/reference/android/database/Cursor.html#moveToFirst()「如果遊標爲空,則此方法將返回false。」 –

+0

該表爲空,遊標必須返回false,但遊標返回count = 1,並在列中使用空值。當我在表中存在DATA且id_person存在時,查詢返回count = 1,因爲該項存在。我沒有想法,這是問題。 –