Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新数据库中的项,但不在ContentValues中设置所有列_Java_Android_Android Contentprovider - Fatal编程技术网

Java 更新数据库中的项,但不在ContentValues中设置所有列

Java 更新数据库中的项,但不在ContentValues中设置所有列,java,android,android-contentprovider,Java,Android,Android Contentprovider,例如,我有四列:first\u name,last\u name,电话号码和图片。在我的代码中的某个地方有: 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(MyPers

例如,我有四列:
first\u name
last\u name
电话号码
图片
。在我的代码中的某个地方有:

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);
我可以运行类似于:

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

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

希望
名字
姓氏
图片
列的值与我第一次设置它们时的值相同。或者我也必须填充
名字
姓氏
图片
列吗?

这将与您描述的完全一样,只会更新设置值。

要回答您的问题,是的,您可以只将一个数据字段传递给更新,它将在不影响任何其他字段的情况下更新该列

从外观上看,您当前遇到的问题是,您没有告诉数据库您要更新哪个记录

按照更新方法:

public int update(字符串表、ContentValues值、字符串where子句、字符串[]wherergs)

您正在为方法的where部分传递null

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


将更新名为tom的任何人的电话号码222-222-2222

您可以在更新函数第三个参数中设置where子句

getContentResolver().update(tedUri, values, "first_name=?", new String
         [ ]{"name"});
getContentResolver().update(tedUri, values, "first_name=?", new String
         [ ]{"name"});