问题
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