《第一行代码》学习笔记29-内容提供器Content Provider(2)

1.查询操作:

if (cursor != null) {
    while (cusor.moveToNext()) {
    String column1 = cursor.getString(cursor.getColumnIndex("column1"));
    int column2 = cursor.getInt(cursor.getColumnIndex("column2"));
}
cursor.close();
}

2.向table1表中添加一条数据:

ContentValues values = new ContentValues();
values.put("column1", "text");
values.put("column2", 1);
getContentResolver().insert(uri, values);

3.若更新这条新添加的数据,把column1的值清空:

ContentValues values = new ContentValues();
values.put("column1", "");
getContentResolver().update(uri, values, "column1 = ? and column2 = ?", new String[] {"text", "1"});

使用selection和selectionArgs参数,对想要更新的数据进行约束,以防止所有行受影响。

4.删除数据:

getContentResolver().delete(uri, "column2 = ?", new String[] {"1"});
原文地址:https://www.cnblogs.com/Iamasoldier6/p/5033198.html