Using insertWithOnConflict to Update or Insert

I need to insert or update, and I found the insertWithOnConflict method in SQLiteDatabase, but I do not know how it checks if a record exists.

Theoretically, I need the Where argument to check if a specific identifier exists, and if so, it should replace all other columns.

This is what I have now, but I don’t think the second argument is a unique id

    ContentValues args = new ContentValues();
    args.put(AppDatabase.COLUMN_ID, entry.getId());
    args.put(AppDatabase.COLUMN_NAME, entry.getAppname());
    args.put(AppDatabase.COLUMN_URL, entry.getAppUrl());
    database.insertWithOnConflict(AppDatabase.TABLE_FILELIST, COLUMN_ID, args, SQLiteDatabase.CONFLICT_REPLACE);

How can I control this behavior?

+4
source share
1 answer
  • Make sure you have the appropriate restriction in your table, for example PRIMARY KEYor UNIQUE.

  • ContentValues. - , , ​​ .

COLUMN_ID PRIMARY KEY. arg nullColumnHack COLUMN_ID , null.

+6

Source: https://habr.com/ru/post/1533400/


All Articles