赞
踩
- /**
- * 内容提供者
- *
- * @author ZhaoKaiQiang
- * @time 2014年6月6日
- */
- public class StudentProvider extends ContentProvider {
- // 数据库操作类,用于获取SQLiteDatabase
- private MyDbOpenHelper dbHelper;
-
- private static final int STUDENT = 1;
- private static final int STUDENTS = 2;
-
- // UriMatcher类是一个很重要的类,因为我们需要根据传入的uri,来判断执行相对应的操作
- private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
-
- // 静态代码块用于初始化MATCHER需要匹配的uri
- static {
- // MATCHER.addURI(主机名(用于唯一标示一个ContentProvider,这个需要和清单文件中的authorities属性相同),路径(可以用来表示我们要操作的数据,路径的构建应根据业务而定),返回值(用于匹配uri的时候,作为匹配的返回值));
- MATCHER.addURI("com.example.mydbdemo.StudentProvider", "student", STUDENTS);
- MATCHER.addURI("com.example.mydbdemo.StudentProvider", "student/#", STUDENT);
- }
-
- // 进行数据的初始化操作
- @Override
- public boolean onCreate() {
- dbHelper = new MyDbOpenHelper(getContext());
- return false;
- }
-
- // 查询
- // 如果uri为 content://com.example.mydbdemo.StudentProvider/student
- // 则代表查询所有的student表内的数据
- // 如果uri为 content://com.example.mydbdemo.StudentProvider/student/6
- // 则代表查询student表内id=6的数据
- @Override
- public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
-
- SQLiteDatabase db = dbHelper.getReadableDatabase();
- //判断传入的uri到底匹配哪一个,从而实现不同的业务需求
- switch (MATCHER.match(uri)) {
- //查询全部的学生信息
- case STUDENTS:
- //db.query(表明, 要查询的列(是一个String数组), where条件, where条件中的参数, groupBy, having, sortOrder);
- return db.query("student", projection, selection, selectionArgs, null, null, sortOrder);
- //查询某一个id对应的学生的信息
- case STUDENT:
- //取出我们要查询的数据的id
- long id = ContentUris.parseId(uri);
- String where = "id=" + id;
- //将selection查询信息拼接到我们的where条件中
- if (selection != null && !"".equals(selection)) {
- where = selection + " and " + where;
- }
- return db.query("student", projection, where, selectionArgs, null, null, sortOrder);
- //如uri不匹配,抛出不合法参数的异常
- default:
- throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
- }
-
- }
-
- // 插入
- @Override
- public Uri insert(Uri uri, ContentValues values) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- switch (MATCHER.match(uri)) {
- case STUDENTS:
- long id = db.insert("student", "name", values);
- return ContentUris.withAppendedId(uri, id);
- default:
- throw new IllegalArgumentException("Uri不匹配");
- }
-
- }
-
- //删除数据
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- int count = 0;
- switch (MATCHER.match(uri)) {
- case STUDENTS:
- count = db.delete("student", selection, selectionArgs);
- return count;
-
- case STUDENT:
- long id = ContentUris.parseId(uri);
- String where = "id=" + id;
- if (selection != null && !"".equals(selection)) {
- where = selection + " and " + where;
- }
- count = db.delete("student", where, selectionArgs);
- return count;
-
- default:
- throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
- }
- }
-
- //更新数据
- @Override
- public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- int count = 0;
- switch (MATCHER.match(uri)) {
- case STUDENTS:
- count = db.update("student", values, selection, selectionArgs);
- return count;
-
- case STUDENT:
- long id = ContentUris.parseId(uri);
- String where = "id=" + id;
- if (selection != null && !"".equals(selection)) {
- where = selection + " and " + where;
- }
- count = db.update("student", values, where, selectionArgs);
- return count;
-
- default:
- throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
- }
- }
-
- // 用于获取MIME Type
- @Override
- public String getType(Uri uri) {
- switch (MATCHER.match(uri)) {
- case STUDENT:
- return "vnd.android.cursor.item/student";
- case STUDENTS:
- return "vnd.android.cursor.dir/student";
- default:
- throw new IllegalArgumentException("Unkwon Uri:" + uri.toString());
- }
-
- }
-
- }
- <!-- 不要忘记exported这个属性,如果不加,可能会导致外部程序访问失败,错误信息为权限拒绝 -->
- <!-- authorities这个属性就是我们在ContentProvider中使用的addURI方法时的第一个参数的取值 -->
- <provider
- android:name="com.example.mydbdemo.StudentProvider"
- android:exported="true"
- android:authorities="com.example.mydbdemo.StudentProvider" >
- </provider>
- //使用ContentProvider添加数据的测试
- public void testadd() throws Throwable {
- //获取ContentResolver对象,完成对ContentProvider的调用
- ContentResolver contentResolver = this.getContext().getContentResolver();
- //构建我们的uir,这个uri
- Uri insertUri = Uri.parse("content://com.example.mydbdemo.StudentProvider/student");
- ContentValues values = new ContentValues();
- values.put("name", "zhaokaikai");
- values.put("age", 91);
- values.put("school", "bbbb");
- //返回值为我们刚插入进入的数据的uri地址
- Uri uri = contentResolver.insert(insertUri, values);
- Log.i(TAG, uri.toString());
- }
- //使用ContentProvider删除数据的测试
- public void testDelete() throws Throwable {
- ContentResolver contentResolver = this.getContext().getContentResolver();
- //删除id为6的学生信息
- Uri deleteUri = Uri.parse("content://com.example.mydbdemo.StudentProvider/student/6");
- contentResolver.delete(deleteUri, null, null);
- }
- //使用ContentProvider更新数据的测试
- public void testUpdate() throws Throwable {
- ContentResolver contentResolver = this.getContext().getContentResolver();
- //更新id = 6 的学生信息
- Uri updateUri = Uri.parse("content://com.example.mydbdemo.StudentProvider/student/6");
- ContentValues values = new ContentValues();
- values.put("name", "testUp");
- values.put("age", "101");
- values.put("school", "ccccc");
- contentResolver.update(updateUri, values, null, null);
- }
- //使用ContentProvider查询数据的测试
- public void testFind() throws Throwable {
- ContentResolver contentResolver = this.getContext().getContentResolver();
- //这个uri用于查询所有的数据,若查询某个id的数据,则构建下面的uri
- //Uri selectUri = Uri.parse("content://com.example.mydbdemo.StudentProvider/student/要查询的id");
- Uri selectUri = Uri.parse("content://com.example.mydbdemo.StudentProvider/student");
- Cursor cursor = contentResolver.query(selectUri, null, null, null, "id desc");
- while (cursor.moveToNext()) {
- int id = cursor.getInt(cursor.getColumnIndex("id"));
- String name = cursor.getString(cursor.getColumnIndex("name"));
- int age = cursor.getInt(cursor.getColumnIndex("age"));
- String school = cursor.getString(cursor.getColumnIndex("school"));
- Log.i(TAG, "id=" + id + ",name=" + name + ",age=" + age +",school="+school);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。