MyContentProvider程序

MySQLiteOpenHelper类

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
   public MySQLiteOpenHelper(Context context) {
       super(context, "person.db", null, 1);
   }

  @Override
  public void onCreate(SQLiteDatabase db) {
      db.execSQL("create table info (_id integer primary key   autoincrement,name varchar(20))");

  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)   {

  }
}

MyContentProvider类

public class MyContentProvider extends ContentProvider {
private static  UriMatcher matcher= new UriMatcher(UriMatcher.NO_MATCH);

private static final int SUCCESS=1;

static {
    matcher.addURI("com.example.mycontentprovider","info",SUCCESS);
}
private MySQLiteOpenHelper myhelper;
public MyContentProvider() {
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {

    if(matcher.match(uri)==SUCCESS){
        SQLiteDatabase db =myhelper.getWritableDatabase();
        int id=db.delete("info",selection,selectionArgs);
        db.close();
        return id;

    }else{
        throw new IllegalArgumentException("路径不匹配");
    }
}

@Override
public String getType(Uri uri) {
    // TODO: Implement this to handle requests for the MIME type of the data
    // at the given URI.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    if(matcher.match(uri)==SUCCESS){

        SQLiteDatabase db = myhelper.getWritableDatabase();
        long id=db.insert("info",null,values);
        if(id>0){
            Uri newUri = ContentUris.withAppendedId(uri,id);
            return newUri;
        }
        db.close();
        return uri;
    }else{
        throw new IllegalArgumentException("路径不匹配");
    }
}

@Override
public boolean onCreate() {
    // TODO: Implement this to initialize your content provider on startup.
    myhelper= new MySQLiteOpenHelper(getContext());
    return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {
    if(matcher.match(uri)==SUCCESS){
        SQLiteDatabase db = myhelper.getReadableDatabase();
        Cursor c= db.query("info",projection,selection,selectionArgs,null,null,sortOrder);
        return c;

    }else{
        throw new IllegalArgumentException("路径不匹配");
    }
}

@Override
public int update(Uri uri, ContentValues values, String selection,
                  String[] selectionArgs) {
    if(matcher.match(uri)==SUCCESS){

        SQLiteDatabase db = myhelper.getWritableDatabase();
        int id=db.update("info",values,selection,selectionArgs);
        db.close();
        return id;
    }else{
        throw new IllegalArgumentException("路径不匹配");
    }
}
}  

MainActivity类

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MySQLiteOpenHelper myhelper = new MySQLiteOpenHelper(this);
    SQLiteDatabase db= myhelper.getWritableDatabase();
    for(int i=0;i<3;i++){
        ContentValues values = new ContentValues();
        values.put("name","abc"+i);
        db.insert("info",null,values);
    }
    db.close();

}
}

AndroidMainifest.xml文件

              

你可能感兴趣的:(MyContentProvider程序)