如果需要,我使用以下方法更新數據庫,數據從網絡服務中檢索爲JSONObject
。但目前看來,這種方法只是停止工作database.update
(日誌這行代碼後,不會出現在logcat的,和logcat的報告沒有錯誤,其怪!)功能剛停止在Android上的更新SQlite數據庫上
private void updateDB(JSONArray serverRecords) throws JSONException {
Log.i(LOGTAG, "update db started");
for (int i = 0; i < serverRecords.length(); i++) {
JSONObject row = serverRecords.getJSONObject(i);
String pin = row.getString("pin");
String state = isRecordUnique(pin);
Log.i(LOGTAG, "State => " + state);
if (state.equals("0")) {
//makeDbReady();
Log.i(LOGTAG, "new record");
// new record
ContentValues inserts = new ContentValues();
inserts.put(SQLiteHelper.COLUMN_CARD_OPERATOR,
row.getInt("opr"));
inserts.put(SQLiteHelper.COLUMN_CARD_TYPE, row.getString("typ"));
inserts.put(SQLiteHelper.COLUMN_CARD_PIN, row.getString("pin"));
inserts.put(SQLiteHelper.COLUMN_CARD_PRICE,
row.getString("pri"));
inserts.put(SQLiteHelper.COLUMN_DATE, row.getString("dte"));
inserts.put(SQLiteHelper.COLUMN_STATE, row.getInt("stt"));
inserts.put(SQLiteHelper.COLUMN_SYNC_DATE, row.getString("syn"));
long affected = database.insert(SQLiteHelper.TABLE_NAME, null,
inserts);
Log.i(LOGTAG, "datbase updated and " + affected
+ " rows affected");
} else {
//makeDbReady();
Log.i(LOGTAG, "update record, sync date => ");
// update existing records
ContentValues values = new ContentValues();
values.put(SQLiteHelper.COLUMN_SYNC_DATE, row.getString("syn"));
values.put(SQLiteHelper.COLUMN_STATE, row.getInt("stt"));
Log.i(LOGTAG, "Values: " + values.toString());
try {
int affected = database.update(SQLiteHelper.TABLE_NAME, values,
SQLiteHelper.COLUMN_ID + " = '" + row.getString("_id")
+ "'", null);
} catch (SQLiteException e) {
Log.i(LOGTAG, e.getMessage());
}
// String sqlQuery = "UPDATE `" + SQLiteHelper.TABLE_NAME
// + "` SET `" + SQLiteHelper.COLUMN_STATE + "` = '"
// + row.getInt("stt") + "', `"
// + SQLiteHelper.COLUMN_STATE + "` = '"
// + row.getInt("stt") + "' " + "WHERE _id ='"
// + row.getString("_id") + "'";
// Log.i(LOGTAG, sqlQuery);
// database.execSQL(sqlQuery);
Log.i(LOGTAG, "update sate: ");
}
Log.i(LOGTAG, "update method: condition did not met");
}
}
我還試圖關閉的Cursor
每個實例在我的代碼中的對象。 唯一可能的「left-open」遊標對象是那些傳遞給處理方法的對象,我不知道它會導致錯誤,但我也評論了調用該方法的代碼行(確保沒有Cursor
對象剩下嘗試更新之前打開)但它沒有任何區別。
下面是日誌:
07-02 16:20:45.307: I/myapp(13928): update db started
07-02 16:20:45.317: I/myapp(13928): State => 4
07-02 16:20:45.317: I/myapp(13928): update record, sync date =>
07-02 16:20:45.317: I/myapp(13928): Values: state=1 sync_date=13-07-02 13:26:31
SQLiteHelper.COLUMN_ID
是一個常數,它的值是:_id
您的IF條件是否正常工作?我的意思是插入部分? –
@benilMathew是的,插入工作正常。 – Nevercom