首页 > 代码库 > Android中内容提供者ContentProvider实现数据库增删改查
Android中内容提供者ContentProvider实现数据库增删改查
1.我们首先new一个我们自己的类集成ContentProvider,并实现方法如下
package com.wzw.sqllitedemo.providers; import com.wzw.sqllitedemo.db.PersonSQLiteOpenHelper; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; public class PersonContentProvider extends ContentProvider { private static UriMatcher uriMatcher; private static final String authority="com.wzw.sqllitedemo.providers.PersonContentProvider"; private static final int PERSON_INSERT_CODE = 0; private static final int PERSON_DELETE_CODE = 1; private static final int PERSON_UPDATE_CODE = 2; private static final int PERSON_QUERYALL_CODE = 3; private PersonSQLiteOpenHelper mOpenHelper; static{ uriMatcher=new UriMatcher(UriMatcher.NO_MATCH); //添加uri(分机号) //content://com.wzw.sqllitedemo.providers.PersonContentProvider/person/insert uriMatcher.addURI(authority, "person/insert", PERSON_INSERT_CODE); uriMatcher.addURI(authority, "person/delete", PERSON_DELETE_CODE); uriMatcher.addURI(authority, "person/update", PERSON_UPDATE_CODE); uriMatcher.addURI(authority, "person/queryAll", PERSON_QUERYALL_CODE); } @Override public boolean onCreate() { mOpenHelper=new PersonSQLiteOpenHelper(getContext()); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { switch (uriMatcher.match(uri)) { case PERSON_QUERYALL_CODE: SQLiteDatabase db=mOpenHelper.getReadableDatabase(); if(db.isOpen()){ Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder); return cursor; // db.close();返回cursor结果集时不能关闭数据库 } break; default: throw new IllegalArgumentException("uri不匹配"); } return null; } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { switch (uriMatcher.match(uri)) { case PERSON_INSERT_CODE: //添加人到person表中 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if (db.isOpen()) { long id = db.insert("person", null, values); db.close(); //返回的类型为content://com.wzw.sqllitedemo.providers.PersonContentProvider/person/insert/id return ContentUris.withAppendedId(uri, id); } break; default: throw new IllegalArgumentException("uri不匹配"); } return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { switch (uriMatcher.match(uri)) { case PERSON_DELETE_CODE: //在person中删除数据 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()){ int count=db.delete("person", selection, selectionArgs); db.close(); return count; } break; default: throw new IllegalArgumentException("uri不匹配"+uri); } return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { switch (uriMatcher.match(uri)) { case PERSON_UPDATE_CODE: //更新person表 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); if(db.isOpen()){ int count=db.update("person", values, selection, selectionArgs); db.close(); return count; } break; default: throw new IllegalArgumentException("uri不匹配"+uri); } return 0; } }
在清单文件中声明
<!-- 清单文件中声明ContextProvider --> <provider android:name="com.wzw.sqllitedemo.providers.PersonContentProvider" android:authorities="com.wzw.sqllitedemo.providers.PersonContentProvider"> </provider>
2.然后我们在新建一个工程来测试
新建一个junit测试类
package com.wzw.other; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.test.AndroidTestCase; import android.util.Log; public class TestCase extends AndroidTestCase { private String tag="TestCase"; public void testInsert(){ //另外一个程序的uri Uri uri = Uri.parse("content://com.wzw.sqllitedemo.providers.PersonContentProvider/person/insert"); //获取内容提供者访问对象 ContentResolver resolver = getContext().getContentResolver(); ContentValues values = new ContentValues(); values.put("name","meimei"); values.put("age", 18); Log.i(tag, "uri"+uri); uri = resolver.insert(uri, values); long id = ContentUris.parseId(uri); Log.i(tag, "插入的id"+id); } public void testDelete(){ //另外一个程序的uri Uri uri = Uri.parse("content://com.wzw.sqllitedemo.providers.PersonContentProvider/person/delete"); //获取内容提供者访问对象 ContentResolver resolver = getContext().getContentResolver(); String where = "_id=?"; String[] selectionArgs={"8"}; int count = resolver.delete(uri, where, selectionArgs); Log.i(tag, "删除了"+count); } public void tesUpdate(){ //另外一个程序的uri Uri uri = Uri.parse("content://com.wzw.sqllitedemo.providers.PersonContentProvider/person/update"); //获取内容提供者访问对象 ContentResolver resolver = getContext().getContentResolver(); ContentValues values=new ContentValues(); values.put("name", "lisi"); int count = resolver.update(uri, values, "_id=?", new String[]{"9"}); Log.i(tag, "更新了:"+count); } public void testQueryAll(){ //另外一个程序的uri Uri uri = Uri.parse("content://com.wzw.sqllitedemo.providers.PersonContentProvider/person/queryAll"); //获取内容提供者访问对象 ContentResolver resolver = getContext().getContentResolver(); Cursor cursor = resolver.query(uri, new String[]{"_id","name","age"}, null, null, "_id"); if(cursor!=null&&cursor.getCount()>0){ int id; String name; int age; while(cursor.moveToNext()){ id=cursor.getInt(0); name=cursor.getString(1); age=cursor.getInt(2); Log.i(tag, "id:"+id+"name:"+name+"age:"+age); } cursor.close(); } } }
把第一个工程部署以后。内容提供者就在手机中存在了,然后用第二个工程来测试他。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。