This article explains how to implement data sharing in the SQLite database, using ContentProvider. The specific operation steps and corresponding Code are as follows:
1. Create a database named MySQLite. Remember to inherit the SQLiteOpenHelper class.
The MySQLite. java code is as follows:
Package com. example. l0828_contentprovider; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper; public class MySqlite extends SQLiteOpenHelper {public static final String TABLE_NAME = "student"; public static final String ID = "_ id"; public static final String NAME = "name "; public static final String SEX = "sex"; // define the SQLite database construction method public MySqlite (Context context) {super (context, TABLE_NAME, null, 1 );} @ Override public void onCreate (SQLiteDatabase db) {// create a table in the database, execSQL method db.exe cSQL ("create table" + TABLE_NAME + "(" + ID + "integer primary key autoincrement," + NAME + "text not null, "+ SEX +" text not null) ") ;}@ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub }}
2. Create a custom MyCP class and inherit the ContentProvider class. The main steps are as follows:
1) inherit the ContentProvider class;
2) Encapsulation of table names and column names in the attribute database;
3) rewrite the corresponding method. The following code is described in detail.) encapsulate the methods used to operate the database;
The MyCPU. java file is as follows:
Package com. example. l0828_contentprovider; import android. content. contentProvider; import android. content. contentValues; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android.net. uri;/** create a class that inherits ContentProvider for packaging database operations to achieve data sharing */public class MyCPU extends ContentProvider {public static final String CP_TABLE_NAME = MySqlite. TABLE_NAME; public static final String CP_ID = MySqlite. ID; public static final String CP_NAME = MySqlite. NAME; public static final String CP_SEX = MySqlite. SEX; private SQLiteDatabase dbReader; private SQLiteDatabase dbWriter; public static final Uri URI = Uri. parse ("content: // com. example. l0828_contentprovider.MyCPU ");/** rewrite the onCreate method **/@ Override public boolean onCreate () {// implement the database instance in the onCreate method, and the database readable and writable object MySqlite my = new MySqlite (getContext (); dbReader = my. getReadableDatabase (); dbWriter = my. getWritableDatabase (); return false;}/** encapsulate the query method **/@ Override public Cursor query (Uri uri, String [] projection, String selection, string [] selectionArgs, String sortOrder) {return dbReader. query (CP_TABLE_NAME, null, selection, selectionArgs, null, null, sortOrder);}/** override the getType method, string used to obtain the Uri **/@ Override public String getType (Uri uri) {return uri. toString ();}/** encapsulation of the insertion method **/@ Override public Uri insert (Uri uri, ContentValues values) {dbWriter. insert (CP_TABLE_NAME, null, values); return uri;}/** encapsulation of the delete operation method **/@ Override public int delete (Uri uri, String selection, string [] selectionArgs) {return dbWriter. delete (CP_TABLE_NAME, selection, selectionArgs);}/** encapsulation of the update operation method **/@ Override public int update (Uri uri, ContentValues values, String selection, string [] selectionArgs) {return dbWriter. update (CP_TABLE_NAME, values, selection, selectionArgs );}}
3. Provider registration:
We all know that the four components of Android need to be registered, and the same is true for providers. The attributes to be registered include:
1) name = "class name that inherits the ContentProvider class"
2) authorites = "package name. Class Name"
The Code registered in this example is as follows:
<provider android:name="MyCPU" android:authorities="com.example.l0828_contentprovider.MyCPU"></provider>
4. Use resource Uri:
A Uri refers to a direct reference to a resource without an address. It is a fixed method different from a URL:
Uri. parse ("content: // package name. Class Name ");
For the code, see MyCP. java.
5. Test Database Operations in the MainActivity. java file:
MainActivity. java:
package com.example.l0828_contentprovider;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener{ private EditText et_name,et_sex; private Button btn_add,btn_select,btn_delete,btn_update; private TextView tv_show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name=(EditText) findViewById(R.id.et_name); et_sex=(EditText) findViewById(R.id.et_sex); btn_add=(Button) findViewById(R.id.btn_add); btn_delete=(Button) findViewById(R.id.btn_delete); btn_update=(Button) findViewById(R.id.btn_update); btn_select=(Button) findViewById(R.id.btn_select); tv_show=(TextView) findViewById(R.id.tv_show); btn_add.setOnClickListener(this); btn_delete.setOnClickListener(this); btn_update.setOnClickListener(this); btn_select.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_add: addDB(); break; case R.id.btn_delete: deleteDB(); break; case R.id.btn_update: updateDB(); break; case R.id.btn_select: selectDB(); break; } } public void addDB(){ ContentValues cv=new ContentValues(); cv.put(MyCPU.CP_NAME,et_name.getText().toString()); cv.put(MyCPU.CP_SEX, et_sex.getText().toString()); MainActivity.this.getContentResolver().insert(MyCPU.URI, cv); } public void deleteDB(){ MainActivity.this.getContentResolver().delete(MyCPU.URI,null , null); } public void updateDB(){ ContentValues cv = new ContentValues(); cv.put(MyCPU.CP_NAME,et_name.getText().toString()); cv.put(MyCPU.CP_SEX, et_sex.getText().toString()); MainActivity.this.getContentResolver().update(MyCPU.URI, null,null, null); } public Cursor selectDB(){ Cursor c=getContentResolver().query(MyCPU.URI, null, null, null, null); while(c.moveToNext()){ tv_show.setText(c.getString(c.getColumnIndex(MyCPU.CP_NAME))+ " "+c.getString(c.getColumnIndex(MyCPU.CP_SEX))); } return c; }}
The running result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1045432621-0.jpg "style =" float: none; "title =" Capture. JPG "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1045433X8-1.jpg "style =" float: none; "title =" Capture 1.JPG"/>
6. Data sharing implementation:
We have only implemented the use of database data in this application. Now we need to truly implement data sharing, that is, applications other than this application can also access and modify data:
Next we will create an application: In MainActivity. java, we will perform simple data operations on the database of the above application:
Package com. example. l0828_using_contentprovider; import android.net. uri; import android. OS. bundle; import android. app. activity; import android. database. cursor; import android. view. menu; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); selectDB ();} public void selectDB () {Cursor c = getContentResolver (). query (Uri. parse ("content: // com. example. l0828_contentprovider.MyCPU "), null); while (c. moveToNext () {// This must be the column name. out. println ("external access:" + c. getString (c. getColumnIndex ("name"); System. out. println ("external access:" + c. getString (c. getColumnIndex ("sex ")));}}}
Result:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/104543N19-2.jpg "title =" Capture. JPG "/>
This article is from the MySpace blog, please be sure to keep this source http://wangzhaoli.blog.51cto.com/7607113/1284567