要记录我在数据库中检查的记录已存在。如果不是INSERT
则执行UPDATE
。但有时提取记录出错,甚至记录存在它找不到任何。为什么?怎么会发生?
仅在此方法中修改了数据库。
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);
}