简体   繁体   中英

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

Found the error in another part of my code where I was erroneously updating everything in the table. doh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM