android在使用sqllite做update操作时,如果要将某个字段值在原来的基础上减少指定的数量,我们会用update语句
update qx_doc set act_qty=act_qty-100 where doc_no=‘001’
有两种方法实现:
方法一:封装好的update方法
ContentValues cv=new ContentValues();
cv.put("act_qty","act_qty-"+Integer.parseInt(selectedDocBarcode.scan_qty));
databaseUtil.db.update("qx_doc",cv,"doc_no=?",new String[]{doc_no});
实践证明这种方法不可取,最后的结果是act_qty="act_qty-100",应该是contentValues put时将值作为字符串处理
方法二、db.execSQL 执行原生sql语句
db.execSQL(String.format("update qx_doc set act_qty=act_qty-100 where doc_no='%s'",new String[]{doc_no}));