mardi 15 décembre 2015

SQLite data base isn't returning the value that was just set

I have a database in which I store user preferences (and a bunch of other stuff). When ever I try and write something to it I have to restart the application (I.E. restart the database) to get the value I just finished inserting. Does anyone know why this is happening? I call insertUnit(false); then immediately getUnit();

public static boolean getUnit(DBHelper base){
    SQLiteDatabase db = base.getWritableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM Preferences where name = 'unit'", null);
    if(res!= null && res.moveToFirst()){
        if(res.getString(res.getColumnIndex("value")).equals("TRUE")) return true;
        else return false;
    }
    return true;
}
public static void insertUnit(boolean unit){
    checkInstance();
    ContentValues contentValues = new ContentValues();

    contentValues.put("value", unit?"TRUE":"FALSE");
    contentValues.put("name", "unit");

    SQLiteDatabase db = mInstance.getWritableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM Preferences WHERE name = 'unit'", null);
    if(res!= null && res.moveToFirst()){
        db.update("Preferences", contentValues, "name = 'unit'", null);
    } else {
        db.insert("Preferences", null, contentValues);
    }
}

Aucun commentaire:

Enregistrer un commentaire