我试图更新SQLite android中的表。我有一个名为' quantity'它存储了一些项目的数量,比如item1,item2 ...
现在当我购买item1时,我绝对想要添加'购买的数量为item1的现有数量
我在网上搜索但无法找到解决方案,因此问这个问题。
我的简单代码如下:
// This method is used to 'UPDATE' the table 'stock'.
// This method will be used by two fragments,
// 'sale' and 'purchase' fragments.
public int updateData(String cigaretteName,int quantity, int cost, int totalCost) {
// Accessing the database with writable functionality so it can be updated.
SQLiteDatabase db = this.getWritableDatabase();
// Creating content values object to put the new values in existing rows with old values.
ContentValues contentValues = new ContentValues();
contentValues.put(StockEntry.COLUMN_QUANTITY, (StockEntry.COLUMN_QUANTITY + quantity));
contentValues.put(StockEntry.COLUMN_COST, cost);
contentValues.put(StockEntry.COLUMN_TOTAL_COST, totalCost);
// Which row to update, based on the cigarette name.
String selection = StockEntry.COLUMN_CIGARETTES_NAME + " LIKE ?";
String[] selectionArgs = {cigaretteName};
// Updating the table with the new values and then returning the number of rows affected.
return db.update(StockEntry.TABLE_NAME, contentValues, selection, selectionArgs);
}
这根本不起作用,现在它甚至不更新列/行。
contentValues.put(StockEntry.COLUMN_QUANTITY, (StockEntry.COLUMN_QUANTITY + quantity));
请帮帮我们!
答案 0 :(得分:0)
我建议采用简单的方法来克服这些与SQLite相关的问题。
以上方式将节省您的开发时间和测试时间。