Decrementing a value not working

那年仲夏 提交于 2019-12-11 10:41:21

问题


I have a column that is to be decremented by 1. The following code doesn't seem to be working.

ContentValues dataToUpdate = new ContentValues();                          
dataToUpdate .put(MARKER_ID, MARKER_ID+"-1");
String where = IMAGE_ID_F+" = " +imageId+ " AND "+MARKER_ID+" > "+markerId+";";
int resultUpdate = db.update(TABLE_DEFECTS, dataToUpdate, where, null);

I also tried rawQuery. That didn't work as well. MARKER_ID is the name of the column.

  String restructureDbQuesry="UPDATE "+TABLE_DEFECTS+" SET "+MARKER_ID+" = "+MARKER_ID+"-1"+" WHERE "+IMAGE_ID_F+" = " +imageId+ " AND "+MARKER_ID+" > "+markerId;
  Cursor resultUpdate = db.rawQuery(restructureDbQuesry,null);

resulting query after an iteration:

UPDATE defects SET Marker_Id = Marker_Id-1 WHERE Image_Id = 2 AND Marker_Id > 2

回答1:


you have to used like this.

 public int updateCounter(int num,String number,int duration, String currentDate){
    ContentValues cv =new ContentValues();
    cv.put(DIALED_CALL, num-1);
    cv.put(CALL_DURATION, duration);
    cv.put(CALL_DATE, currentDate);
     return db.update(TABLE_LOGS, cv, PHONE_NUMBER + " = ? ", new String []{number});        

}

try this also change in your code.

dataToUpdate .put(MARKER_ID, MARKER_ID-1);



回答2:


There is a problem with this line:

dataToUpdate .put(MARKER_ID, MARKER_ID+"-1");

If MARKER_ID is the name of the column (lets assume its 'markerid') then you will set the column to the string value "markerid-1".




回答3:


Try this:

String restructureDbQuesry="UPDATE "+TABLE_DEFECTS+" SET "+MARKER_ID+" = "+MARKER_ID+"-1"+" WHERE "+IMAGE_ID_F+" = " +imageId+ " AND "+MARKER_ID+" > "+markerId;
db.execSQL(restructureDbQuesry,null);



回答4:


Apparently, after you execute queries that return nothing in the cursor, we have to perform some operation to the cursor.

So, resultUpdate.moveToFirst(); solved the issue.



来源:https://stackoverflow.com/questions/14099756/decrementing-a-value-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!