ContentResolver 更新数据库

可能很多人会被ContentResolver.update方法的四个参数弄晕;尤其是后两个参数;

今天就以下面数据库中这张表为例,对其中的UserID和Password的值进行更新;

TIM图片20180108154614.png
private void updateUserId(){

    ContentResolver resolver =mContext.getContentResolver();

    Uri uri =Uri.parse(Uri);

    ContentValues DeviceCode=new ContentValues();

    DeviceCode.put("name", LogIn_username_key);

    DeviceCode.put("value",userId);

    resolver.update(uri, DeviceCode, "name = ?", new String[]{"UserID"});

}

private void insertPassword(){

    ContentResolver resolver =mContext.getContentResolver();

    Uri uri =Uri.parse(LogIn_Uri);

    Log.i(TAG,"mPassword = "+userPassword);

    ContentValues DeviceCode=new ContentValues();

    DeviceCode.put("name", LogIn_password_key);

    DeviceCode.put("value",userPassword);

    resolver.update(uri, DeviceCode, "name = ?", new String[]{"Password"});

}

可以看到,resolver.update的
第一个参数,uri用来找到指定的数据库和表;

第三和第四个参数, 表示找到name = "Password"的那一行;
当然也可以加多个定位参数,比如"name = ? and _id = ?" new String[]{"Password","0"}
这样找到的就是id=0同时键名是Password的那一行了;

第二个参数则是要更新的数据了,只要通过上面三个参数确定了行数,那么这一行的任何一列数值都可以修改;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。