简体   繁体   中英

Android SQLite relative increase of column value

Using SQLite db.update function, we can modify a record giving absolute values for records satisfying where clause. For ex:

ContentValues cv = new ContentValues();
cv.put("id",111);
cv.put("Field1",10);
cv.put("Field2",100);

But, how do I say Field1 = Field1 + 5, without using a separate select query to fetch existing Field1 value or using db.execSQL(updateQuery) ? (I deliberately want to avoid db.execSQL for update operations)

I want to avoid a separate query because, if multiple threads perform update on same record, there could be concurrency issues.

Use the execSQL() method of SQLiteDatabase :

String increment = "UPDATE "
        + YOUR_TABLE + " SET "
        + YOUR_COLUMN + " = "
        + YOUR_COLUMN + " + 5";

db.execSQL(increment);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM