jeudi 18 février 2016

Inconsistent android sqlite database check

I am trying to check if an entry in an sqlite android database exists. My code works when the Id's are unique but when there are duplicate Ids with different suspensionUnit values, things are inconsistent. On the first try my code returns false regardless of whats in the db i.e. even when it should return true it always returns false the first time. Trying again, however returns true and continues to return true so long as I am in the same fragment. Now, if I try checking for another entry with the same Id but different suspensionUnit I will get false on the first attempt and then true again and again. If I now try the first entry (the one we started with) it goes back to false again. ARGH

public Boolean getGear(String id, String suspensionUnit) {
        SQLiteDatabase db = this.getReadableDatabase();

        String Query = "Select * from " + TABLE_GEAR
                + " where " + KEY_GEAR_ID + " = \"" + id
                + "\" and " + KEY_SUSPENSION_UNIT + " = \"" + suspensionUnit + "\";";
        Cursor cursor = db.rawQuery(Query, null);
        if(cursor.getCount() <= 0){
            cursor.close();
            return false;
        }
        cursor.close();
        return true;
    }

Aucun commentaire:

Enregistrer un commentaire