在安卓系统上,UPDATE SQlite数据库的功能刚刚停止



如果需要,我使用以下方法更新数据库,数据作为JSONObject从web服务中检索。但这种方法似乎在database.update上停止了工作(这行代码之后的日志没有出现在logcat上,并且logcat报告没有错误,这很奇怪!)

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

将数据类型从int affected更改为long affected

所以你的代码应该像这个

long affected = database.update(SQLiteHelper.TABLE_NAME, values,
                    SQLiteHelper.COLUMN_ID + " = '" + row.getString("_id")
                            + "'", null);

希望它能在上运行

最新更新