首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

ContentProvider 重复插入解决方法

2012-09-29 
ContentProvider 重复插入解决办法?重复插入 解决办法:??1. 因为ContentProvider 所拥有的函数接口 比如:i

ContentProvider 重复插入解决办法

?重复插入 解决办法:

?

?

1. 因为ContentProvider 所拥有的函数接口 比如:insert delete update onCreate 都已经固定 不支持自定义 所以我打算以patch的形式

?

?

?2. 函数原型

public class ResolverUtility {/* - insertUnique *  - arguments: *  * cr : ContentResolver instance to use *  * uri : target w/ Uri format to insert *  * cv : ContentValues to insert *  * uid : unique column to replace *   *  - return:null */public static void insertUnique(ContentResolver cr, Uri uri, ContentValues cv, String uid){String scase = (String)cv.get(uid);String selection = "name = '"+scase+"'";Cursor c = cr.query(uri, null, selection, null, null);if(c.getCount() > 0){cr.update(uri, cv, selection, null);}else {cr.insert(uri, cv);}}}

?

?

2. 原理:

其实 原理很简单 即:在新插入数据之前 先查询指定列的值 并以之为条件 查询是否存在 该数据如果存在 则调用update() 方法更新目标如果不存在 则调用insert()方法插入之

?

?

3. 如何使用:

ContentValues uvalues = new ContentValues();                uvalues.put(CHelper.NAME, "griffinshi");        uvalues.put(CHelper.NUMBER, 7686);        uvalues.put(CHelper.HOME, "Jiangsu");                ResolverUtility.insertUnique(cResolver, CHelper.CONTENT_URI, uvalues, CHelper.NAME);        

?

?

?

结束!

热点排行