Android SQL更新查询返回1但不更新

时间:2017-02-04 20:47:55

标签: java android sql-update android-sqlite

我试图按照此处的文档更新SQL表中的简单布尔值:Updating DB Row, developer.android.com

当我尝试更新表时,update语句返回1,但在关闭并重新打开应用程序后,数据库保持不变。这是代码:

在MainActivity.java中:

 void CheckUncheckTodayTask (String taskID, boolean newState) {
    new CheckUncheckTodayTask().execute(taskID, String.valueOf(newState));
 }

 class CheckUncheckTodayTask extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... strings) {
        SQLiteDatabase db = mDbHelper.getReadableDatabase();

        // New value for one column
        ContentValues values = new ContentValues();
        values.put(TaskContract.TodayTask.COLUMN_NAME_COMPLETED, Boolean.valueOf(strings[1]));

        // Which row to update, based on the title
        String selection = TaskContract.TodayTask._ID+ " = ?";
        String[] selectionArgs = { strings[0] };

        db.update(
                TaskContract.TodayTask.TABLE_NAME,
                values,
                selection,
                selectionArgs);

        return null;
    }

}

在TodayTaskAdapter.java中:

mCompletedCheckBox = (CheckBox) view.findViewById(R.id.cb_today_completed);
mCompletedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            mCompletedCheckBox.setPaintFlags(mCompletedCheckBox.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            mCompletedCheckBox.setTextColor(Color.GRAY);
            ((MainActivity)activityContext).CheckUncheckTodayTask(String.valueOf((long)mCompletedCheckBox.getTag(R.id.tag_check_box)), true);
        } else {
            mCompletedCheckBox.setPaintFlags(mCompletedCheckBox.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
            mCompletedCheckBox.setTextColor(Color.BLACK);
            ((MainActivity)activityContext).CheckUncheckTodayTask(String.valueOf((long)mCompletedCheckBox.getTag(R.id.tag_check_box)), false);
        }
    }
});

确实调用了click处理程序,并在检查时传递了taskID值1和newState值true。

我尝试过更改为使用getWriteableDatabase,在where子句中使用LIKE而不是=,逐行逐步执行代码,但没有抛出任何错误。

谢谢!

1 个答案:

答案 0 :(得分:0)

执行查询后,应调用SqliteDatabase setTransactionSuccessful()endTransaction()close()函数; 否则您的更新查询将不会生效,

这是我的例子:

SQLiteDatabase db=this.getWritableDatabase(); // open writable database
db.beginTransaction();

try {
    ContentValues values = new ContentValues();
    values.put(KEY_OID, order.getOrderId());
    values.put(KEY_STATUS, order.getStatus());
    values.put(KEY_ORDER_NUM,order.getOrderNum());
    values.put(KEY_ESHOP_CAT, order.getEShopCat());
    values.put(KEY_REFERENCE,order.getReference());
    values.put(KEY_REJECT_REASON,order.getRejectReason());
    values.put(KEY_CANCEL_REASON,order.getCancelReason());
    values.put(KEY_PICKUP_TIMESTAMP, TimeUtil.getDatetimeFromUnixTimestamp(order.getOrderReadyTimestamp()));

    int rows = db.update(TABLE_ORDERS, values, KEY_OID+" = ?", new String[]{order.getOrderId()});

    if(rows==1) { // check if update succeeded
       Log.d(TAG, "update current order success");
    } else {
       long rowId=db.insert(TABLE_ORDERS,null, values);
       if(rowId!=-1){
           Log.d(TAG, "add new order success");
       } else {
           throw new Exception("error occurred while insert order");
       }
    }

    } catch(Exception e) {
       Log.d(TAG, "Error: "+e.toString());
    } finally {
       // IMPORTANT : must set transaction successful after query done, otherwise changes wont take effect especially update query
       db.setTransactionSuccessful();
       db.endTransaction();
       db.close();
}