title: SQLite auto increment on INSERT and UPDATE
body: <p>I have a SQLite database and I need to mark any new record and any modified record with an ever increasing counter. I have to avoid race conditions</p>
  
  <p>Those are my two queries I'm using at the moment:</p>
  
  <p><strong>Insert:</strong></p>
  
  <pre><code>INSERT INTO customer (id, name, counter) VALUES (myId, "blablabla", ((SELECT MAX(counter) FROM customer) + 1))
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <pre><code>UPDATE customer SET name = "blablabla", counter = (SELECT MAX(counter) FROM customer) + 1 WHERE id = myId
  </code></pre>
  
  <p>Is this safe? I'm using SQLite on a Cordova mobile app</p>
  
  <p>If not, what's the best way to do it? Can I declare <code>counter</code> as <code>AUTO_INCREMENT</code> and have just those two queries?</p>
  
  <p><strong>Insert:</strong></p>
  
  <pre><code>INSERT INTO customer (id, name) VALUES (myId, "blablabla")
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <pre><code>UPDATE customer SET name = "blablabla", counter = (SELECT MAX(counter) FROM customer) + 1 WHERE id = myId
  </code></pre>