title: SQLite instability in Android?
body: <p>To upsert a record I check in database wether record already exist. If not <code>INSERT</code> ootherwise <code>UPDATE</code> is performed. But sometimes fetching record goes wrong, and even record exist <strong>it does not find any</strong>. Why? How can it happen?</p>
  
  <p>DB is modified only in this method.</p>
  
  <pre><code>11-04 13:08:34.542 20181-20248/com.j4nos.moviebuffs W/System.err: android.database.sqlite.SQLiteConstraintException: column _id is not unique (code 19)
  11-04 13:08:34.548 20181-20248/com.j4nos.moviebuffs W/System.err:     at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
  11-04 13:08:34.550 20181-20248/com.j4nos.moviebuffs W/System.err:     at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:779)
  11-04 13:08:34.551 20181-20248/com.j4nos.moviebuffs W/System.err:     at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
  11-04 13:08:34.553 20181-20248/com.j4nos.moviebuffs W/System.err:     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
  11-04 13:08:34.554 20181-20248/com.j4nos.moviebuffs W/System.err:     at com.j4nos.moviebuffs.Utility$2.run(Utility.java:360)
  11-04 13:08:34.556 20181-20248/com.j4nos.moviebuffs W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  11-04 13:08:34.557 20181-20248/com.j4nos.moviebuffs W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  11-04 13:08:34.558 20181-20248/com.j4nos.moviebuffs W/System.err:     at java.lang.Thread.run(Thread.java:838)
  </code></pre>
  
  <hr>
  
  <p><code>public static ExecutorService serialQueue = Executors.newFixedThreadPool(1);</code> </p>
  
  <pre><code>public static void upsert(final JSONObject record) {
  
          Runnable runnable = new Runnable() {
              public synchronized void run() {
  
                  try {
                      String recordType = record.getString("recordType");
                      String id = record.getString("_id");
  
                      Boolean recordExist = true;
                      Cursor cursor3 = Utility.db.rawQuery("SELECT * FROM " + recordType + " WHERE _id = '" + id + "'", null);
                      if (cursor3.getCount() == 0) {
                          recordExist = false;
                      }
                  //.....
              }
          };
  
  
      Utility.serialQueue.execute(runnable);
  }
  </code></pre>