withValueBackReference的理解
2014-11-01 16:06
274 查看
转自:http://blog.csdn.net/hudashi/article/details/7076656
Android中在进行批量操作数据库时,有一个方法很难理解,经过长时间琢磨,将心得分享如下:
[java] view
plaincopy
<span style="white-space:pre"> </span>int backRef = ops.size();
Log.d(TAG, "backRef: " + backRef);
ContentValues values = new ContentValues();
ops.add(ContentProviderOperation
.newInsert(RawContacts.CONTENT_URI)
// .withValues(values).build());
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, backRef)
.withValues(getPeopleCV()).build());
if (nickName != null) {
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, backRef)
.withValue(Data.MIMETYPE, Nickname.CONTENT_ITEM_TYPE)
.withValue(Nickname.NAME, getDecodeValue(nickName)).build());
}
if (birthday != null) {
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, backRef)
.withValue(Data.MIMETYPE, Event.CONTENT_ITEM_TYPE)
.withValue(Event.TYPE, Event.TYPE_BIRTHDAY)
.withValue("data1", getDecodeValue(birthday)).build());
}
操作通讯录时,需要先在RawContact表中插入记录,然后根据改记录的rawContactId,向Data表中插入该联系人的具体信息。如果你将这两种数据库操作进行批处理的时候,你无法知道插入成功后rawContactId到底是多少,于是android提供了这个方法:
[java] view
plaincopy
withValueBackReference(Data.RAW_CONTACT_ID, backRef)
[java] view
plaincopy
<pre name="code" class="java"></pre>
<pre></pre>
backRef指的是:第backRef条批处理数据执行完成后,生成的索引值,将作为当前这条记录的Data.RAW_CONTACT_ID字段的值。从而解决了批量插入联系人信息时无法拿到rawContactId的问题。
Android中在进行批量操作数据库时,有一个方法很难理解,经过长时间琢磨,将心得分享如下:
[java] view
plaincopy
<span style="white-space:pre"> </span>int backRef = ops.size();
Log.d(TAG, "backRef: " + backRef);
ContentValues values = new ContentValues();
ops.add(ContentProviderOperation
.newInsert(RawContacts.CONTENT_URI)
// .withValues(values).build());
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, backRef)
.withValues(getPeopleCV()).build());
if (nickName != null) {
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, backRef)
.withValue(Data.MIMETYPE, Nickname.CONTENT_ITEM_TYPE)
.withValue(Nickname.NAME, getDecodeValue(nickName)).build());
}
if (birthday != null) {
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, backRef)
.withValue(Data.MIMETYPE, Event.CONTENT_ITEM_TYPE)
.withValue(Event.TYPE, Event.TYPE_BIRTHDAY)
.withValue("data1", getDecodeValue(birthday)).build());
}
操作通讯录时,需要先在RawContact表中插入记录,然后根据改记录的rawContactId,向Data表中插入该联系人的具体信息。如果你将这两种数据库操作进行批处理的时候,你无法知道插入成功后rawContactId到底是多少,于是android提供了这个方法:
[java] view
plaincopy
withValueBackReference(Data.RAW_CONTACT_ID, backRef)
[java] view
plaincopy
<pre name="code" class="java"></pre>
<pre></pre>
backRef指的是:第backRef条批处理数据执行完成后,生成的索引值,将作为当前这条记录的Data.RAW_CONTACT_ID字段的值。从而解决了批量插入联系人信息时无法拿到rawContactId的问题。
相关文章推荐
- 个人对withValueBackReference的理解
- withValueBackReference
- withValueBackReference简介
- Java call by value 和call by reference 的理解
- C# - test and cast with reference type or value...
- 【Python】ValueError: invalid literal for int() with base 10的错误
- The reference to entity "characterEncoding" must end with the ';' delimiter.
- Android studio b Error: No resource found that matches the given name (at 'src' with value '@dra...
- 《Working with Unix Processes》-理解Unix进程笔记
- java报错:The reference to entity "characterEncoding" must end with the ';' delimiter.
- Tip/Trick: ASP.NET 2.0 Deal with DBNull value when bind to RadioButtonList/当绑定RadioButtonL
- 安装libvirt-snmp过程及报错undefined reference to `virDomainCreateWithFlags'
- Java EE 7 / JAX-RS 2.0: Simple REST API Authentication & Authorization with Custom HTTP Header--reference
- How to deal with "Incorrect NSStringEncoding value 0×0000 Error with ASIHTTPRequest" problem?
- 【url】The reference to entity "characterEncoding" must end with the ';' delimiter
- The reference to entity "characterEncoding" must end with the ';' delimiter
- 读书笔记《Effective c++》 条款20 宁以pass-by-reference-toconst替换pass-by-value
- 奇怪的异常 Process 'command '---.exe'' finished with non-zero exit value 2
- c++如何理解map对象的value_type是pair类型
- 安卓常见错误处理记录5:安卓系统编译报错 finished with non-zero exit value -1073741819