使用事务操作android 数据库
来源:互联网 发布:怎样用c语言编写游戏 编辑:程序博客网 时间:2024/05/16 12:00
使用事务添加联系人
1. 在添加联系人得时候是分多次访问Provider,如果在过程中出现异常,会出现数据不完整的情况,这些操作应该放在一次事务中
2. 使用ContentResolver的applyBatch(String authority,ArrayList<ContentProviderOperation> operations) 方法可以将多个操作在一个事务中执行
示例:
//使用事务添加联系人
public void testInsertBatch() throws Exception {
ContentResolver resolver = getContext().getContentResolver();
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
ContentProviderOperation operation1 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/raw_contacts")) //
.withValue("_id", null) //
.build();
operations.add(operation1);
ContentProviderOperation operation2 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/data")) //
.withValueBackReference("raw_contact_id", 0) //
.withValue("data2", "ZZH") //
.withValue("mimetype", "vnd.android.cursor.item/name") //
.build();
operations.add(operation2);
ContentProviderOperation operation3 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/data")) //
.withValueBackReference("raw_contact_id", 0) //
.withValue("data1", "18612312312") //
.withValue("data2", "2") //
.withValue("mimetype", "vnd.android.cursor.item/phone_v2") //
.build();
operations.add(operation3);
ContentProviderOperation operation4 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/data")) //
.withValueBackReference("raw_contact_id", 0) //
.withValue("data1", "www.2cto.com") //
.withValue("data2", "2") //
.withValue("mimetype", "vnd.android.cursor.item/email_v2") //
.build();
operations.add(operation4);
// 在事务中对多个操作批量执行
resolver.applyBatch("com.android.contacts", operations);
}
1. 在添加联系人得时候是分多次访问Provider,如果在过程中出现异常,会出现数据不完整的情况,这些操作应该放在一次事务中
2. 使用ContentResolver的applyBatch(String authority,ArrayList<ContentProviderOperation> operations) 方法可以将多个操作在一个事务中执行
示例:
//使用事务添加联系人
public void testInsertBatch() throws Exception {
ContentResolver resolver = getContext().getContentResolver();
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
ContentProviderOperation operation1 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/raw_contacts")) //
.withValue("_id", null) //
.build();
operations.add(operation1);
ContentProviderOperation operation2 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/data")) //
.withValueBackReference("raw_contact_id", 0) //
.withValue("data2", "ZZH") //
.withValue("mimetype", "vnd.android.cursor.item/name") //
.build();
operations.add(operation2);
ContentProviderOperation operation3 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/data")) //
.withValueBackReference("raw_contact_id", 0) //
.withValue("data1", "18612312312") //
.withValue("data2", "2") //
.withValue("mimetype", "vnd.android.cursor.item/phone_v2") //
.build();
operations.add(operation3);
ContentProviderOperation operation4 = ContentProviderOperation //
.newInsert(Uri.parse("content://com.android.contacts/data")) //
.withValueBackReference("raw_contact_id", 0) //
.withValue("data1", "www.2cto.com") //
.withValue("data2", "2") //
.withValue("mimetype", "vnd.android.cursor.item/email_v2") //
.build();
operations.add(operation4);
// 在事务中对多个操作批量执行
resolver.applyBatch("com.android.contacts", operations);
}
- 使用事务操作android 数据库
- android 使用数据库事务提高操作效率
- android数据库事务操作
- Android数据库事务操作
- android数据库事务操作
- Android数据库事务操作
- Android 数据库事务操作
- Android中的数据库操作及内置SQLite使用和事务
- Android学习_18_使用事务操作SQLite数据库
- android数据库事务操作详解
- android数据库 批量 事务 操作
- android数据库 批量 事务 操作
- android数据库 批量 事务 操作
- 使用事务操作SQLite数据库
- 使用事务操作SQLite数据库
- 使用事务操作SQLite数据库
- 使用事务操作SQLite数据库
- 使用事务操作SQLite数据库
- android 多媒体和相机详解三 .
- tcprstat工具安装与使用
- AHCI
- Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead
- 一个“奇怪”的Web项目
- 使用事务操作android 数据库
- http 上传中止
- 2013新交规扣分细则全文
- Unity 数据管理 [转自羽化]
- 登陆网站的python脚本
- 机器学习入门
- 新闻发布系统 (Asp.net 三层架构 )
- Linux Platform Device and Driver的注册过程解析
- UT-Exynos4412 三星ARM四核旗舰开发平台android4.0体验-8摄像头功能调试支持