public class DatabaseProvider extends ContentProvider {
public static UriMatcher uriMatcher;
private MyDatabaseHelper databaseHelper;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY,"book",BOOK_DIR);
uriMatcher.addURI(AUTHORITY,"book/#",BOOK_ITEM);
uriMatcher.addURI(AUTHORITY,"category",CATEGORY_DIR);
uriMatcher.addURI(AUTHORITY,"category/#",CATEGORY_ITEM);
}
@Override
public boolean onCreate() {
databaseHelper = new MyDatabaseHelper(getContext(),"BookStore.db",null,304);
return true;
}
}
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new MyDatabaseHelper(this,"BookStore.db",null,304);
}
}
创建的数据库和内容提供器里面提供的数据库版本号必须一致,否则的话就会报版本错误;也就是DatabaseProvider里面的onCreate方法中的版本号要和MainActivity中的onCreat方法版本号一致