1

I want to check if a couple of values exists in the database. If they do, the method should return TRUE or FALSE if the cursor is null. But the problem is that it returns TRUE all the time despite the values are not in the database! What have I missed?

    // This method check if the combination image path and contact name already exists in database
public boolean checkContentDatabase(String imageFilePath, String contactName) {

    String query = "Select * from " + DB_TABLE+ " where " + TABLE_IMAGE_PATH + "='" + imageFilePath + "' and " + TABLE_CONTACT_NAME + "='" + contactName +"' ;";

    Cursor c = db.rawQuery(query, null);

    if(c != null) // Exists in database
     {
     return true;
     }
     else
     {
         return false;
     }
}
4

4 に答える 4

5

if 条件を次のように置き換えます。

if(c.getCount() > 0)
 {
 return true;
 }
 else
 {
     return false;
 }
于 2013-03-05T08:25:22.927 に答える
3

c != null && c.getCount() > 0の代わりに使用c != null

于 2013-03-05T08:23:26.910 に答える
2
      public boolean checkContentDatabase(String imageFilePath, String contactName) {

String query = "Select * from " + DB_TABLE+ " where " + TABLE_IMAGE_PATH      + "='"          +          imageFilePath + "' and " + TABLE_CONTACT_NAME + "='" + contactName +"' ;";

Cursor c = db.rawQuery(query, null);


           c.moveToFirst();
    if(c.isAfterLast() == false) {
       return true;
      }
     else
       {
      return false;
      }
     }
于 2013-03-05T08:25:25.633 に答える
1

カーソルは正常に作成されたため、nullではありません。結果セットのみが存在しません。「SELECT1FROM...」を使用して、結果が1に等しいかどうかを確認してください。

于 2013-03-05T08:21:45.953 に答える