ContentProvider(一)

1、 SQLiteOpenHelper类的创建
数据库表的创建(onCreate)

public class DbHelper extends SQLiteOpenHelper{
    private static final String name ="mydb.db";
    private static final int version=1;
    public DbHelper(Context context) {
        super(context, name, null, version);
        
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql="create table student (id integer primary key autoincrement,name varchar(64) ,address varchar(64))";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        
    }

}


2、ContentProvider类的创建(StudentProvider)
(1)在xml文件中添加provider属性:name 、authorities

```
    
    
```

(2)定义TAG、DbHelper;定义UriMacher;定义两个标识位(STUDENT、STUDENTS)
(3)static{}静态模块:
添加匹配规则:addUri();

```
static {
    //addURI(String authority, String path, int code)
    //path: the path to match. * may be used as a wild card for any text,
    // and # may be used as a wild card for numbers.
    URI_MATCHER.addURI("com.example.sevenprovider_laoluo.StudentProvider", "student", STUDENTS);
    URI_MATCHER.addURI("com.example.sevenprovider_laoluo.StudentProvider", "student", STUDENT);
}
```

3、重写getType:

public String getType(Uri uri) {
    int flag = URI_MATCHER.match(uri);
    switch (flag) {
    case STUDENT:
        return "vnd.android.cursor.item/student";
    case STUDENTS:
        return "vnd.android.cursor.dir/students";
    }
    return null;
}

4、插入数据
(1)实例化helper:

public boolean onCreate() {
    helper = new DbHelper(getContext());
    return false;
}

(2)利用insert插入数据

@Override
public Uri insert(Uri uri, ContentValues values) {
    Uri resultUri = null;
    int flag = URI_MATCHER.match(uri);
    switch (flag) {
    case STUDENTS:
        SQLiteDatabase database = helper.getWritableDatabase();
        // return:the row ID of the newly inserted row, or -1 if an error
        // occurred
        long id = database.insert("student", null, values);// 插入当前行的行号
        // Appends the given ID to the end of the path.
        resultUri = ContentUris.withAppendedId(uri, id);
        break;
    }
    Log.i(TAG, "-------" + resultUri.toString());
    return resultUri;
}

5、建立MyTest.java测试类:

public class MyTest extends AndroidTestCase{

    public MyTest() {
        
    }
    public void insert(){
        
        //内容解析者
        ContentResolver contentResolver=getContext().getContentResolver();
        //Creates a Uri which parses the given encoded URI string.
        Uri url=Uri.parse("content://com.example.sevenprovider_laoluo.StudentProvider/student");
        ContentValues values=new ContentValues();
        values.put("name", "jerry");
        values.put("address", "Shanghai");
        contentResolver.insert(url, values);//url :The URL of the table to insert into.

    }

}

附:

package com.example.sevenprovider_laoluo;

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;
import android.util.Log;

public class StudentProvider extends ContentProvider {
    private final String TAG = "StudentProvider";
    private DbHelper helper = null;

    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int STUDENT = 1;
    private static final int STUDENTS = 2;

    static {
        URI_MATCHER.addURI("com.example.sevenprovider_laoluo.StudentProvider", "student", STUDENTS);
        URI_MATCHER.addURI("com.example.sevenprovider_laoluo.StudentProvider/#", "student", STUDENT);
    }

    @Override
    public boolean onCreate() {
        helper = new DbHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getType(Uri uri) {
        int flag = URI_MATCHER.match(uri);
        switch (flag) {
        case STUDENT:
            return "vnd.android.cursor.item/student";

        case STUDENTS:
            return "vnd.android.cursor.dir/students";
        }
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Uri resultUri = null;
        int flag = URI_MATCHER.match(uri);
        switch (flag) {
        case STUDENTS:
            SQLiteDatabase database = helper.getWritableDatabase();
            // return:the row ID of the newly inserted row, or -1 if an error
            // occurred
            long id = database.insert("student", null, values);// 插入当前行的行号
            // Appends the given ID to the end of the path.
            resultUri = ContentUris.withAppendedId(uri, id);
            break;

        }
        Log.i(TAG, "-------" + resultUri.toString());
        return resultUri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }

}

你可能感兴趣的:(ContentProvider(一))