简体   繁体   中英

Android SQLite Update not working

I am trying to update one column for any number of rows.

Here is the function:

public void setAwardsSyncComplete(String[] ids) {

    String inArray = StringUtils.separateCommas(ids);
    db.beginTransaction();

    try {
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_SYNCED, true);

        int rowsAffected = db.update(TABLE, contentValues, COL_ID + " IN (" + inArray + ")", null);

    } catch (Exception e) {

        DebugLog.e("Error in transaction", e.toString());
    } finally {

        db.endTransaction();
    }
}

What is strange is that the rowsAffected returns correctly (ie rowsAffected > 0), but the column values remain null .

Am I overlooking something very simple here?

Thanks.

As you're using transactions, you need to call db.setTransactionSuccessful(); at the end of the try clause. Without this, the update gets rolled back.

See SQLiteDatabase.beginTransaction

Hope this helps,

Phil Lello

你需要调用db.setTransactionSuccussful()db.update否则任何更改都将回滚,当你调用endTransaction()

there's no explicit boolean type in sqlite tables? what data type is the COL_SYNED column you are trying to update?

and you will need to call db.setTransactionSuccussful()

I think there is a problem on your update..

You need to loop your array and update each one by one..

    private int _rowsAffected;

    foreach (var a in inArray)
    {

    _rowsAffected= db.update(TABLE, contentValues, COL_ID + " = (" + a +")", null);

    }

    db.Commit();
    db.setTransactionSuccussful(); 


if(_rowsAffected > 0)
//Success

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM