简体   繁体   English

Android SQLite更新无法正常工作

[英]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 . 奇怪的是rowsAffected正确返回(即rowsAffected> 0),但列值保持为null

Am I overlooking something very simple here? 我在这里忽略了一些非常简单的事情

Thanks. 谢谢。

As you're using transactions, you need to call db.setTransactionSuccessful(); 在使用事务时,需要调用db.setTransactionSuccessful(); at the end of the try clause. 在try子句的末尾。 Without this, the update gets rolled back. 如果没有这个,更新将回滚。

See SQLiteDatabase.beginTransaction 请参见SQLiteDatabase.beginTransaction

Hope this helps, 希望这可以帮助,

Phil Lello Phil Lello

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

there's no explicit boolean type in sqlite tables? 在sqlite表中没有明确的布尔类型? what data type is the COL_SYNED column you are trying to update? 您要更新的COL_SYNED列是什么数据类型?

and you will need to call db.setTransactionSuccussful() 你需要调用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 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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