To upsert a record I check in database wether record already exist. If not INSERT
ootherwise UPDATE
is performed. But sometimes fetching record goes wrong, and even record exist it does not find any. Why? How can it happen?
DB is modified only in this method.
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)
public static ExecutorService serialQueue = Executors.newFixedThreadPool(1);
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);
}