ContentProvider 对SQLite数据共享

来源:互联网 发布:网络维护工具套装 编辑:程序博客网 时间:2024/06/11 03:17

对数据库进行共享,首先对数据库的创建:

public class DBHelper extends SQLiteOpenHelper {public DBHelper(Context context, int version) {super(context, "wz.db", null, version);}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubdb.execSQL("create table student (_id int, name varchar(200) ,age int)");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("drop table student");db.execSQL("create table student (_id int primary key, name varchar(200) ,age int)");}}

接着对该数据库进行数据共享操作,实现数据的CRUD操作:

public class StudentProvider extends ContentProvider {private DBHelper helper;private static int STUDENTS =1;private static int STUDENT =2;private SQLiteDatabase db;private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);static{matcher.addURI("com.wpf.student.provider", "students", STUDENTS);matcher.addURI("com.wpf.student.provider", "student/#", STUDENT);}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {int num = 0;switch (matcher.match(uri)) {case 1:num = db.delete("student", selection, selectionArgs);break;case 2:long rowId = ContentUris.parseId(uri);String where = "_id =" +rowId;while (selection != null && !"".equals(selection.trim())) {where += "and" + selection;}num = db.delete("student", where, selectionArgs);break;}return num;}@Overridepublic String getType(Uri uri) {// TODO Auto-generated method stubreturn null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {switch (matcher.match(uri)) {case 1:long rowid = db.insert("student", "name", values);Uri insertUri = ContentUris.withAppendedId(uri,rowid);return insertUri;}return null;}@Overridepublic boolean onCreate() {helper = new DBHelper(this.getContext(), 2);db = helper.getWritableDatabase();return false;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {Cursor cursor = null;switch (matcher.match(uri)) {case 1:cursor = db.query("student", projection,selection, selectionArgs, "", "", "");break; case 2:long rowId = ContentUris.parseId(uri);String where = "_id ="+rowId;while(selection != null && !"".equals(selection.trim())){ where += "and " + selection;}cursor = db.query("student", projection,selection, selectionArgs, "", "", "");}return cursor;}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {int num = 0;switch (matcher.match(uri)) {case 1:db.update("student", values, selection, selectionArgs);break;case 2:long rowId = ContentUris.parseId(uri);String where = "_id =" +rowId;while (selection != null && !"".equals(selection.trim())) {where += "and" + selection;}num = db.update("student", values, where, selectionArgs);break;}return num;}}

由于ContentProvider为android四大组件,所以必须进行注册:

<provider android:name=".StudentProvider"             android:authorities="com.wpf.student.provider"            android:exported="true"/>

以上三步实现了A程序的数据共享操作,B程序可以对A程序进行操作:

public class MainActivity extends Activity {private Button add,query,delete,update;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);add = (Button) findViewById(R.id.button1);query = (Button) findViewById(R.id.button2);delete = (Button) findViewById(R.id.button3);update = (Button) findViewById(R.id.button4);add.setOnClickListener(new listen());query.setOnClickListener(new listen());delete.setOnClickListener(new listen());update.setOnClickListener(new listen());}class listen implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubContentResolver resolver = getContentResolver();;switch (v.getId()) {case R.id.button1:Uri uri = Uri.parse("content://com.wpf.student.provider/students");ContentValues values = new ContentValues();values.put("_id", "3");values.put("name", "wz2");values.put("age", "24");resolver.insert(uri, values);Toast.makeText(MainActivity.this, "OK", 1000).show();break;case R.id.button2:Uri uri1 = Uri.parse("content://com.wpf.student.provider/student/1");Cursor cursor = resolver.query(uri1, new String[]{"_id","name","age"}, null, null, "");while(cursor.moveToNext()){Log.i("DATA", cursor.getInt(0)+","+cursor.getString(1)+","+cursor.getInt(2));}break;case R.id.button3:Uri uri2 = Uri.parse("content://com.wpf.student.provider/student/1");resolver.delete(uri2, null, null);case R.id.button4:Uri uri3 = Uri.parse("content://com.wpf.student.provider/student/2");ContentValues values2 = new ContentValues();values2.put("name", "wz");resolver.update(uri3, values2, null, null);}}}}

B程序通过URI对A程序数据进行共享操作,实现了数据共享和访问的同步性。

界面如图所示:

运行结果:

OK,ContentProvide 完成,其中,ContentProvide就像http的域名一样,而ContentResolver就比如访问链接,其中的ContentUris帮助类的

long rowId = ContentUris.parseId(uri);

Uri insertUri = ContentUris.withAppendedId(uri,rowid);

是非常重要和常用的方法!

原创粉丝点击