Inserting data to SQLite table with constraint failure
I'm trying to insert data into SQLite table on Android. _id
is primary key of the table and I am inserting a row using this method:
public void addSomeData(int id, String datetime) {
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, id);
contentValues.put(KEY_DATETIME, datetime);
mDb.insert(TABLE, null, contentValues);
}
The problem I get is that sometimes primary key constraint is validated and I would like to use something like INSERT IF NOT EXISTS
, but preferably something that would work with ContentValues
. What are my options? I understand that insertOrThrow()
and insertWithOnConflict()
methods only return different values, or should I use one of these methods?