title: SQLite UPSERT / UPDATE OR INSERT
body: <p>I need to perform UPSERT / INSERT OR UPDATE against a SQLite Database.</p>
  
  <p>There is the command INSERT OR REPLACE which in many cases can be useful. But if you want to keep your id's with autoincrement in place because of foreign keys, it does not work since it deletes the row, creates a new one and consequently this new row has a new ID.</p>
  
  <p>This would be the table:</p>
  
  <p>players - (primary key on id, user_name unique)</p>
  
  <pre><code>|  id   | user_name |  age   |
  ------------------------------
  |  1982 |   johnny  |  23    |
  |  1983 |   steven  |  29    |
  |  1984 |   pepee   |  40    |
  </code></pre>