一、在AndroidManifest.xml文件中添加

<providerandroid:name=".StudentProvider"android:authorities="com.example.android_contentprovider2.StudentProvider"></provider>

二、创建数据库SqliteOpenHelper

publicclassDbHelperextendsSQLiteOpenHelper{privatestaticStringname="mydb.db";privatestaticintversion=1;//初始的版本号是一publicDbHelper(Contextcontext){super(context,name,null,version);//TODOAuto-generatedconstructorstub}@OverridepublicvoidonCreate(SQLiteDatabasedatabase){//TODOAuto-generatedmethodstubStringsql="createtablestudent(idintegerprimarykeyautoincrement,namevarchar(64),addressvarchar(64))";database.execSQL(sql);//对数据库的表的创建}@OverridepublicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){//TODOAuto-generatedmethodstub}}


三、创建ContentProvider

publicclassStudentProviderextendsContentProvider{privatefinalStringTAG="StudentProvider";privateDbHelperhelper=null;privatestaticfinalUriMatcherURI_MATCHER=newUriMatcher(UriMatcher.NO_MATCH);privatestaticfinalintSTUDENT=1;//操作单条记录privatestaticfinalintSTUDENTS=2;//操作多条记录static{URI_MATCHER.addURI("com.example.android_contentprovider2.StudentProvider","student",STUDENTS);URI_MATCHER.addURI("com.example.android_contentprovider2.StudentProvider","student/#",STUDENT);}publicStudentProvider(){//TODOAuto-generatedconstructorstub}@Overridepublicintdelete(Uriuri,Stringselection,String[]selectionArgs){//TODOAuto-generatedmethodstubintcount=-1;//影响数据库的行数try{intflag=URI_MATCHER.match(uri);SQLiteDatabasedatabase=helper.getWritableDatabase();switch(flag){caseSTUDENT://content://com.example.android_contentprovider2.StudentProvider/student/1//deletefromstudentwhereid=?//id通过客户端传递过来的longid=ContentUris.parseId(uri);Stringwhere_value="id="+id;if(selection!=null&&!selection.equals("")){where_value+="and"+selection;}count=database.delete("student",where_value,selectionArgs);break;caseSTUDENTS:count=database.delete("student",selection,selectionArgs);break;}}catch(Exceptione){//TODO:handleexception}returncount;}@OverridepublicStringgetType(Uriuri){//TODOAuto-generatedmethodstubintflag=URI_MATCHER.match(uri);switch(flag){caseSTUDENT:return"vnd.android.cursor.item/student";caseSTUDENTS:return"vnd.android.cursor.dir/students";}returnnull;}@OverridepublicUriinsert(Uriuri,ContentValuesvalues){//TODOAuto-generatedmethodstub//insertintostudent()(?,?);UriresultUri=null;intflag=URI_MATCHER.match(uri);switch(flag){caseSTUDENTS:SQLiteDatabasedatabase=helper.getWritableDatabase();longid=database.insert("student",null,values);//插入当前行的行号resultUri=ContentUris.withAppendedId(uri,id);break;}Log.i(TAG,"---->>"+resultUri.toString());returnresultUri;}@OverridepublicbooleanonCreate(){//TODOAuto-generatedmethodstubhelper=newDbHelper(getContext());returntrue;}@OverridepublicCursorquery(Uriuri,String[]projection,Stringselection,String[]selectionArgs,StringsortOrder){//TODOAuto-generatedmethodstubCursorcursor=null;try{SQLiteDatabasedatabase=helper.getReadableDatabase();intflag=URI_MATCHER.match(uri);switch(flag){caseSTUDENT:longid=ContentUris.parseId(uri);Stringwhere_value="id="+id;if(selection!=null&&!selection.equals("")){where_value+="and"+selection;}cursor=database.query("student",null,where_value,selectionArgs,null,null,null,null);break;caseSTUDENTS:cursor=database.query("student",null,selection,selectionArgs,null,null,null);break;}}catch(Exceptione){//TODO:handleexception}returncursor;}@Overridepublicintupdate(Uriuri,ContentValuesvalues,Stringselection,String[]selectionArgs){//TODOAuto-generatedmethodstubintcount=-1;try{//updatetablesetname=?,address=?whereid=?SQLiteDatabasedatabase=helper.getWritableDatabase();longid=ContentUris.parseId(uri);intflag=URI_MATCHER.match(uri);switch(flag){caseSTUDENT:Stringwhere_value="id="+id;if(selection!=null&&!selection.equals("")){where_value+="and"+selection;}count=database.update("student",values,where_value,selectionArgs);break;}}catch(Exceptione){//TODO:handleexception}returncount;}}


附件:http://down.51cto.com/data/2364447