Android数据库的批处理操作(以添加联系人为例)
来源:互联网 发布:淘宝网书 编辑:程序博客网 时间:2024/05/16 07:30
Batch operations
Data rows can be inserted/updated/deleted using the traditional insert(Uri, ContentValues)
,update(Uri, ContentValues, String, String[])
and delete(Uri, String, String[])
methods, however the newer mechanism based on a batch ofContentProviderOperation
will prove to be a better choice in almost all cases. All operations in a batch are executed in a single transaction, which ensures that the phone-side and server-side state of a raw contact are always consistent. Also, the batch-based approach is far more efficient: not only are the database operations faster when executed in a single transaction, but also sending a batch of commands to the content provider saves a lot of time on context switching between your process and the process in which the content provider runs.
The flip side of using batched operations is that a large batch may lock up the database for a long time preventing other applications from accessing data and potentially causing ANRs ("Application Not Responding" dialogs.)
To avoid such lockups of the database, make sure to insert "yield points" in the batch. A yield point indicates to the content provider that before executing the next operation it can commit the changes that have already been made, yield to other requests, open another transaction and continue processing operations. A yield point will not automatically commit the transaction, but only if there is another request waiting on the database. Normally a sync adapter should insert a yield point at the beginning of each raw contact operation sequence in the batch. See withYieldAllowed(boolean)
.
Operations
- Insert
An individual data row can be inserted using the traditional
insert(Uri, ContentValues)
method. Multiple rows should always be inserted as a batch.An example of a traditional insert:
ContentValues values = new ContentValues(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, "1-800-GOOG-411"); values.put(Phone.TYPE, Phone.TYPE_CUSTOM); values.put(Phone.LABEL, "free directory assistance"); Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
The same done using ContentProviderOperations:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(Data.RAW_CONTACT_ID, rawContactId) .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, "1-800-GOOG-411") .withValue(Phone.TYPE, Phone.TYPE_CUSTOM) .withValue(Phone.LABEL, "free directory assistance") .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
- Update
Just as with insert, update can be done incrementally or as a batch, the batch mode being the preferred method:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)}) .withValue(Email.DATA, "somebody@android.com") .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
- Delete
Just as with insert and update, deletion can be done either using the
delete(Uri, String, String[])
method or using a ContentProviderOperation:ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI) .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)}) .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
- Query
- Finding all Data of a given type for a given contact
Cursor c = getContentResolver().query(Data.CONTENT_URI, new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL}, Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", new String[] {String.valueOf(contactId)}, null);
- Finding all Data of a given type for a given raw contact
Cursor c = getContentResolver().query(Data.CONTENT_URI, new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL}, Data.RAW_CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", new String[] {String.valueOf(rawContactId)}, null);
- Finding all Data for a given raw contact
- Most sync adapters will want to read all data rows for a raw contact along with the raw contact itself. For that you should use the
ContactsContract.RawContactsEntity
. See alsoContactsContract.RawContacts
.
以添加联系人为例:
- protected void createContactEntry() {
- ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
- ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
- .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, mSelectedAccount.getType())
- .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, mSelectedAccount.getName())
- .build());
- ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
- .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
- .withValue(ContactsContract.Data.MIMETYPE,
- ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
- .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "小明")
- .build());
- ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
- .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
- .withValue(ContactsContract.Data.MIMETYPE,
- ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
- .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "10086")
- .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, "1")
- .build());
- ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
- .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
- .withValue(ContactsContract.Data.MIMETYPE,
- ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
- .withValue(ContactsContract.CommonDataKinds.Email.DATA, "google@sina.com")
- .withValue(ContactsContract.CommonDataKinds.Email.TYPE, "1")
- .build());
- try {
- getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
- } catch (Exception e) {
- Log.e(TAG, "Exceptoin encoutered while inserting contact: " + e);
- }
- }
这里withValueBackReference("column","index");第一个参数对应于数据库中的列,第二个参数代表着回引的值。这个比较抽象,也就是说,批量操作数据库时,index表示ArrayList<ContentProviderOperation> ops 这个操作列表中,第几个操作返回结果的id或是数量。
例如:上面的代码withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);这个表示将ContactsContract.Data.RAW_CONTACT_ID的值设为以下插入操作执行完毕后返回的uri中Id的值。
- ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, mSelectedAccount.getType()) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, mSelectedAccount.getName()) .build()
- Android数据库的批处理操作(以添加联系人为例)
- Android数据库的批处理操作(以添加联系人为例)
- 关于检测手机内部存储的方法(以内置存储空间几乎为0M时添加联系人为例)
- 以获取和添加联系人为例学习调用系统ContentPrivider
- Android数据库的批处理操作
- syncML 服务器端 同步算法 (以联系人为例)
- JDBC操作数据库的批处理
- 防止人为误操作MySQL数据库技巧一例
- 王晓东-以人为“基站”的网络
- 社交工程师的日常工作:以人为目标
- 防止人为误操作MySQL数据库技巧
- Test2.数据库批处理添加练习(mysql_java)
- 批处理操作Oracle数据库
- 批处理操作mysql数据库
- JDBC 数据库批处理操作
- ubuntu下用qt访问服务器(以图灵机器人为例)
- Android中,对SQLite的‘批处理’操作
- mybatis集成spring操作数据库(以注解的方式)
- Android特效 五种Toast详解
- python对象类型(1)
- wget 用法笔记
- 抽象类可以继承实体类吗?
- linux安装和命令
- Android数据库的批处理操作(以添加联系人为例)
- 问题
- java web中实现同一帐号同一时间只能一个地点登陆(类似QQ登录的功能)
- Android的语言设置
- 看文档遇到的生词
- 学习 xilinx fpga 一
- C++的不足之处讨论系列(五)
- flex现实表情文字发送功能
- Access导出一句话拿shell实战