1.什么Content Provider内容提供者:管理被访问的结构化数据——数据库
底层封装的是数据库的操作,操作数据库A应用中将本地数据库的数据分享给B的应用,必须要使用内容提供者
内容提供者就是提供数据的数据源,给客户端访问。
2.内容 提供者提供了一套对外接口的访问规则——————跨应用
query 查询
insert 插入
update修改
delete删除
清单文件中加入:
<provider
android:name=".PersonContentProvider"
android:authorities="com.example.android.PersonContentProvider" >
</provider>
---------------------------------------DBHelper---------------------------------------------------
1 public class DBHelper extends SQLiteOpenHelper { 2 private static String name = "mydb.db"; 3 private static int version = 1; 4 5 public DBHelper(Context context) { 6 super(context, name, null, version); 7 // TODO Auto-generated constructor stub 8 } 9 10 @Override 11 public void onCreate(SQLiteDatabase db) { 12 // TODO Auto-generated method stub 13 String sql = "create table person(_id integer primary key autoincrement,name varchar(64),address varchar(64))"; 14 db.execSQL(sql); 15 } 16 17 @Override 18 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 19 // TODO Auto-generated method stub 20 21 } 22 23 }
-----------------------------------PersonContentProvider------------------------------------
1 public class PersonContentProvider extends ContentProvider { 2 private DBHelper helper; 3 private final static UriMatcher URI_MATCHER = new UriMatcher( 4 UriMatcher.NO_MATCH); 5 private final static int PERSONS = 1;// 操作多条记录 6 private final static int PERSON = 2;// 操作单行记录 7 static { 8 // 表示往UriMatcher添加匹配规则 9 // *:字符#:数字 10 // authority:清单文件中的authorities; path:表名, code 11 12 URI_MATCHER.addURI("com.example.android.PersonContentProvider", 13 "person", PERSONS); 14 // com.example.android.PersonContentProvider/person/张" 15 URI_MATCHER.addURI("com.example.android.PersonContentProvider", 16 "person/#", PERSON); 17 // com.example.android.PersonContentProvider/person/1" 18 19 } 20 21 @Override 22 public boolean onCreate() { 23 // TODO Auto-generated method stub 24 helper = new DBHelper(getContext()); 25 return false; 26 } 27 28 @Override 29 public Cursor query(Uri uri, String[] projection, String selection, 30 String[] selectionArgs, String sortOrder) { 31 // TODO Auto-generated method stub 32 // SQLiteDatabase database=helper.getReadableDatabase(); 33 // database.query(table, columns, selection, selectionArgs, groupBy, 34 // having, orderBy); 35 Cursor cursor = null; 36 int flag = URI_MATCHER.match(uri); 37 SQLiteDatabase database = helper.getReadableDatabase(); 38 39 switch (flag) { 40 case PERSON: 41 long _id = ContentUris.parseId(uri); 42 String where_value = "_id =" + _id; 43 if (selection != null && !selection.equals("")) { 44 where_value += selection; 45 } 46 cursor = database.query("person", projection, where_value, 47 selectionArgs, null, null, sortOrder); 48 break; 49 50 case PERSONS: 51 cursor = database.query("person", projection, selection, 52 selectionArgs, null, null, sortOrder); 53 break; 54 } 55 return cursor; 56 } 57 58 @Override 59 public String getType(Uri uri) { 60 // TODO Auto-generated method stub 61 // 解析uri,判断mime类型 62 int flag = URI_MATCHER.match(uri); 63 switch (flag) { 64 case PERSON: 65 66 return "vnd.android.cursor.item/person"; 67 68 case PERSONS: 69 return "vnd.android.cursor.dir/persons"; 70 } 71 return null; 72 } 73 74 @Override 75 public Uri insert(Uri uri, ContentValues values) { 76 // TODO Auto-generated method stub 77 Uri result = null; 78 // 解析uri 79 int flag = URI_MATCHER.match(uri); 80 switch (flag) { 81 case PERSONS: 82 // 调用数据库的访问方法 83 SQLiteDatabase database = helper.getReadableDatabase(); 84 long id = database.insert("person", null, values); 85 result = ContentUris.withAppendedId(uri, id);// 生成的uri返回 86 System.out.println("--->>" + result.toString()); 87 break; 88 89 } 90 91 return result; 92 } 93 94 // delete from person where _id=? and name=? 95 // content://com.example.android.PersonContentProvider/person/1 96 @Override 97 public int delete(Uri uri, String selection, String[] selectionArgs) { 98 // TODO Auto-generated method stub 99 int flag = URI_MATCHER.match(uri); 100 SQLiteDatabase database = helper.getWritableDatabase(); 101 int count = 0;// 影响数据库的行数 102 switch (flag) { 103 case PERSON: 104 long _id = ContentUris.parseId(uri); 105 String where_value = "_id= " + _id; 106 if (selection != null && !selection.equals("")) { 107 where_value += selection; 108 109 } 110 count = database.delete("person", where_value, selectionArgs); 111 break; 112 113 case PERSONS: 114 count = database.delete("person", selection, selectionArgs); 115 break; 116 } 117 return count; 118 } 119 120 // update person set name=? and address=? where _id=? 121 @Override 122 public int update(Uri uri, ContentValues values, String selection, 123 String[] selectionArgs) { 124 // TODO Auto-generated method stub 125 int flag = URI_MATCHER.match(uri); 126 int count = 0;// 影响数据库行数 127 SQLiteDatabase database = helper.getWritableDatabase(); 128 switch (flag) { 129 case PERSON: 130 long _id = ContentUris.parseId(uri); 131 String where_value = "_id=" + _id; 132 if (selection != null && !selection.equals("")) { 133 where_value += selection; 134 } 135 count = database.update("person", values, where_value, 136 selectionArgs); 137 break; 138 139 } 140 return count; 141 } 142 143 }
------------------------------------- MyTest------------------------------
1 public class MyTest extends AndroidTestCase { 2 3 public MyTest() { 4 // TODO Auto-generated constructor stub 5 } 6 7 public void add() { 8 // 使用内容解析者访问 9 ContentResolver contentResolver = getContext().getContentResolver(); 10 ContentValues values = new ContentValues(); 11 values.put("name", "xx"); 12 values.put("address", "xxx"); 13 // content://authorities/person 14 Uri uri = Uri 15 .parse("content://com.example.android.PersonContentProvider/person"); 16 contentResolver.insert(uri, values); 17 18 } 19 20 public void del() { 21 ContentResolver contentResolver = getContext().getContentResolver(); 22 // Uri 23 // 删掉单条: 24 // uri=Uri.parse("content://com.example.android.PersonContentProvider/person/1"); 25 Uri uri = Uri 26 .parse("content://com.example.android.PersonContentProvider/person"); 27 Uri new_uri = ContentUris.withAppendedId(uri, 1); 28 contentResolver.delete(new_uri, null, null); 29 // delect from person where _id=1 and name=xx,and address=xxx 30 // contentResolver.delete(new_uri, "and name=? and address=?", new 31 // String[]{"xx","xxx"});//如果_id不是主键 32 33 // 全删 34 Uri uri2 = Uri 35 .parse("content://com.example.android.PersonContentProvider/person"); 36 37 contentResolver.delete(uri2, null, null); 38 } 39 40 41 public void qurey() { 42 ContentResolver contentResolver = getContext().getContentResolver(); 43 Uri uri = Uri 44 .parse("content://com.example.android.PersonContentProvider/person"); 45 Uri new_uri = ContentUris.withAppendedId(uri, 2); 46 47 Cursor cursor = contentResolver.query(new_uri, null, null, null, null); 48 while(cursor.moveToNext()){ 49 50 System.out.println("-->>" 51 + cursor.getString(cursor.getColumnIndex("name"))); 52 } 53 } 54 55 }