title: Why does insertWithOnConflict(..., CONFLICT_IGNORE) return -1 (error)?
body: <p>I have an SQLite table:</p>
  
  <pre><code>CREATE TABLE regions (_id INTEGER PRIMARY KEY, name TEXT, UNIQUE(name));
  </code></pre>
  
  <p>And some Android code:</p>
  
  <pre><code>Validate.notBlank(region);
  ContentValues cv = new ContentValues();
  cv.put(Columns.REGION_NAME, region);
  long regionId = 
      db.insertWithOnConflict("regions", null, cv, SQLiteDatabase.CONFLICT_IGNORE);
  Validate.isTrue(regionId &gt; -1,
      "INSERT ON CONFLICT IGNORE returned -1 for region name '%s'", region);
  </code></pre>
  
  <p>On duplicate rows insertWithOnConflict() is returning -1, indicating an error, and <a href="http://commons.apache.org/lang/api/org/apache/commons/lang3/Validate.html#isTrue%28boolean,%20java.lang.String,%20java.lang.Object...%29" rel="noreferrer">Validate</a> then throws with:</p>
  
  <pre><code>INSERT ON CONFLICT IGNORE returned -1 for region name 'Overseas'
  </code></pre>
  
  <p>The <a href="http://sqlite.org/lang_conflict.html" rel="noreferrer">SQLite ON CONFLICT documentation</a> (emphasis mine) states:</p>
  
  <blockquote>
    <p>When an applicable constraint violation occurs, the IGNORE resolution algorithm skips the one row that contains the constraint violation and continues processing subsequent rows of the SQL statement as if nothing went wrong. Other rows before and after the row that contained the constraint violation are inserted or updated normally. <em>No error is returned when the IGNORE conflict resolution algorithm is used.</em></p>
  </blockquote>
  
  <p>The <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="noreferrer">Android insertWithOnConflict() documentation</a> states:</p>
  
  <blockquote>
    <p>Returns
    the row ID of the newly inserted row OR the primary key of the existing row if the input param 'conflictAlgorithm' = CONFLICT_IGNORE OR -1 if any error </p>
  </blockquote>
  
  <p>CONFLICT_REPLACE isn't an option, because replacing rows will change their primary key instead of just returning the existing key:</p>
  
  <pre><code>sqlite&gt; INSERT INTO regions (name) VALUES ("Southern");
  sqlite&gt; INSERT INTO regions (name) VALUES ("Overseas");
  sqlite&gt; SELECT * FROM regions;
  1|Southern
  2|Overseas
  sqlite&gt; INSERT OR REPLACE INTO regions (name) VALUES ("Overseas");
  sqlite&gt; SELECT * FROM regions;
  1|Southern
  3|Overseas
  sqlite&gt; INSERT OR REPLACE INTO regions (name) VALUES ("Overseas");
  sqlite&gt; SELECT * FROM regions;
  1|Southern
  4|Overseas
  </code></pre>
  
  <p>I think that insertWithOnConflict() should, on duplicate rows, return me the primary key (_id column) of the duplicate row &mdash; so I should <em>never</em> receive an error for this insert. Why is insertWithOnConflict() throwing an error? What function do I need to call so that I always get a valid row ID back?</p>