Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>INSERT OR REPLACE</code> works just like <code>ON CONFLICT REPLACE</code>. It will delete the row if the row with the unique column already exists and than it does an insert. It never does update.</p> <p>I would recommend you stick with your current solution, you create table with <code>ON CONFLICT</code> clausule, but every time you insert a row and the constraint violation occurs, your new row will have new <code>_id</code> as origin row will be deleted.</p> <p>Or you can create table without <code>ON CONFLICT</code> clausule and use <code>INSERT OR REPLACE</code>, you can use <a href="http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insertWithOnConflict%28java.lang.String,%20java.lang.String,%20android.content.ContentValues,%20int%29" rel="nofollow">insertWithOnConflict()</a> method for that, but it is available since API level 8, requires more coding and leads to the same solution as table with <code>ON CONFLICT</code> clausule.</p> <p>If you still want to keep your origin row, it means you want to keep the same <code>_id</code> you will have to make two queries, first one for inserting a row, second to update a row if insertion failed (or vice versa). To preserve consistency, you have to execute queries in a transaction.</p> <pre><code> db.beginTransaction(); try { long rowId = db.insert(table, null, values); if (rowId == -1) { // insertion failed String whereClause = "latitude=? AND longitude=?"; String[] whereArgs = new String[] {values.getAsString("latitude"), values.getAsString("longitude")}; db.update(table, values, whereClause, whereArgs); // now you have to get rowId so you can return correct Uri from insert() // method of your content provider, so another db.query() is required } db.setTransactionSuccessful(); } finally { db.endTransaction(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload