游标查询在mCount中返回1

时间:2016-08-24 17:40:43

标签: android sqlite

我在SQLITE Android中有一个查询:

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
      }
    }

这是查询的结果(所有值都为null,表为空):

enter image description here

问题是,返回count = 1,但我的数据库中没有任何记录,我的表是空的,并且光标认为存在1条记录,但问题是Cursor的所有值都是NULL

是否需要任何其他配置来检查查询是否有结果? 添加转储光标:

Dumping cursor android.content.ContentResolver$CursorWrapperInner@24371d87
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
}

1 个答案:

答案 0 :(得分:1)

在我看来,您有一行id_person_patientid_blood_type的空值。如果该表为空,则moveToFirst()将返回false,显然该计数不会为> 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,所以如果count为0,它必须返回false。总之,如果getCount()返回0,moveToPosition()不能返回true。