未在ContentValues中设置所有列的情况下更新数据库中的项目 [英] Update item in database without all of the columns set in ContentValues

查看:144
本文介绍了未在ContentValues中设置所有列的情况下更新数据库中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有四列:first_namelast_namephone_numberpicture.在我的代码中的某个地方:

For example, I have four columns: first_name, last_name, phone_number and picture. Somewhere in my code there's:

ContentValues values = new ContentValues();
values.put(MyPerson.FIRST_NAME, "Ted");
values.put(MyPerson.LAST_NAME, "Johnson");
values.put(MyPerson.PHONE_NUMBER, "111-111-1111");
values.put(MyPerson.PICTURE, "file://tedpicture.jpeg");

getContentResolver().update(tedUri, values, null, null);

我可以运行类似的内容吗

Could I run something like:

ContentValues values = new ContentValues();
values.put(MyPerson.PHONE_NUMBER, "222-222-2222");

getContentResolver().update(tedUri, values, null, null);

并且希望first_namelast_namepicture列具有与我第一次设置它们时相同的值.还是我也必须填充first_namelast_namepicture列?

And expect the first_name,last_name, and picture columns to have the same values as when I first set them. Or do I have to also populate the first_name, last_name, and picture columns too?

推荐答案

要回答您的问题,是的,您可以仅将一个数据字段传递给更新,它将更新该列,而不会影响其他任何列.

To answer your question, yes you can pass just one field of data to the update and it will update that column without affecting any others.

从外观上看,您当前遇到的问题不是在告诉数据库要更新的记录.

The problem you currently have, by the looks of it, you are not telling the DB which record you want to update.

按照更新方法:

public int update (String table, ContentValues values, String whereClause, String[] whereArgs)

您将方法的where部分传递为null.

You are passing null for the where part of the method.

如果将where部分传递给方法,它将仅更新特定条目.

If you pass a where part to the method it will update just the specific entry(s).

ContentValues values = new ContentValues();
values.put(MyPerson.PHONE_NUMBER, "222-222-2222");
getContentResolver().update(tedUri, values, "first_name = ?", new String[] { "tom" });

会更新名字为 tom 的任何人,使其电话号码为222-222-2222

Would update anyone with the first name tom to have the phone number 222-222-2222

这篇关于未在ContentValues中设置所有列的情况下更新数据库中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆