如何在Android内容提供商处使用此查询?
update tblTot set vMax = vMax + 15 where _id = 1;
其中vMax是tblTot表的列。
我试过这样做
Uri totUri = Uri.parse(DbProvider.TOT_CONTENT_URI + "/1");
ContentValues valoriTotali = new ContentValues();
valoriTotali.put(DbHelper.TBTOT_VMAX, DbHelper.TBTOT_VMAX + " + " + newVMax);
int rcdTot = resolver.update(totUri, valoriTotali, null, null);
但它不起作用,因为此更新将值设置为字符串(“db列名+值”)而不是我的db行中的整数。
提前谢谢
答案 0 :(得分:1)
最好为它公开新的URI(或在URI中传递其他参数)并在此基础上采取适当的操作在ContentProvider的update()中,类似这样 -
case CASE_URI_INCREMENT_LIKE:
String sqlStatement = "UPDATE " + Table.TABLE_NAME + " SET " + Table.VALUE + " = " + Table.VALUE + " + 1 WHERE " + Table.ID + "=?";
mSQLiteDatabase.execSQL(sqlStatement, selectionArgs);
break;
答案 1 :(得分:0)
您可以获取vMax
的值并将其转换为Integer
// CAST((CAST(vMax as INTEGER) +15) AS text)
编辑:如何使用上述行
db = this.getReadableDatabase();
String selectQuery = "select vMax from tblTot "
+ " where _id = 1";
Cursor cursor = db.rawQuery(selectQuery, null);
int vMax = 0;
if (cursor.moveToFirst()) {
Log.i(Tag, "vMax: "+cursor.getString(0) );
vMax= Integer.parseInt(cursor.getString(0));
}
ContentValues values = new ContentValues();
values.put("vMax", Integer.toString(vMax+15));
db.update("tblTot ", values, _id + " = ?", new String[] { String.valueOf(_id ) });
db.Close();