mardi 3 mai 2016

Values are inserted again and again in the SQLite database

I have a table with 4 columns and I want to perform inert else update operation whenever i am inserting data. I want to check based on "clues_values" .

Here is my table

 ID  ANSWER_NUMBER ANSWER_VALUES CLUES_NUMBERS CLUES_VALUES

 1    20             apple          20            red
 2    50             banana         50            yellow
 3    60             orange         60            orange
 4    90             grapes         90            black

My insert statement will be like,

db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY , ANSWER_NUMBER INTEGER, ANSWER_VALUES TEXT, CLUES_NUMBERS INTEGER , CLUES_VALUES TEXT)");

In order to do a insert else update , I just wrote the below code

    public boolean InsertVal(int ansnum, String ansval, int cluenum, String clueval) { // these are the arguements
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(C1, ansnum);
    contentValues.put(C2, ansval);
    contentValues.put(C3, cluenum);
    contentValues.put(C4, clueval);
    long result = db.insertWithOnConflict(TABLE_NAME, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    if (result == -1) {
        return false;

    } else {
        return true;
    }
  }

But it is inserting the records in the sqlite database each time I run the code. The values are DYNAMIC VALUES so , I don't know how to check other than using 'insertWithOnConflict' . I referred a lot of stack overflow questions but noting helped me. Any help would be great ! Thanks !!

Aucun commentaire:

Enregistrer un commentaire