【问题标题】:update column with different values sqlite database android studio用不同的值更新列sqlite数据库android studio
【发布时间】:2017-11-15 12:25:26
【问题描述】:

我有一个包含 7 列的隐藏数据库,其中一列是价格,其值为 null。我想在 id = 1 的字段列中输入一些值,然后对于 id = 2 我输入另一个值等,当我填写价格列时,我想更新整个值。那可能吗? 我的代码只更新一个值

public boolean updateData(String id,String name, String number_phone, String about, Integer price, String color){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, id);
    contentValues.put(COL_2, name);
    contentValues.put(COL_3, number_phone);
    contentValues.put(COL_4, about);
    contentValues.put(COL_5, price);
    contentValues.put(COL_6, color);

    db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{id});
    return true;
}

【问题讨论】:

    标签: android android-studio sqlite android-sqlite


    【解决方案1】:

    我不太明白你的问题,但据我了解,这可能会对你有所帮助。

    如果您只想更新一个字段值,则需要使用 for 循环。 在 onCreate 方法中,使用要在表的每一行中更新的价格数组调用它。 价格数组不应大于否。表中存在的行数。

    for(int i=0;i<(no. of rows in table);i++){
    updateData(rowId,priceArray[i]);
    }
    

    现在更新方法会是这样的。

    public boolean updateData(String id, Integer price){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_5, price);
        db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{id});
        return true;
    }
    

    如果您想更新某些行,例如 {1,4,5},那么您必须通过调用带有 id 和价格的 updateData 方法来一一更新。

    但是,如果您希望在 {1,4,5} 等某些行中更新相同的价格字段值,则可以

    public boolean updateData(Integer price){
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(COL_5, price);
            db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{1,4,5});
            return true;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      相关资源
      最近更新 更多