一、 创建应用程序并且继承一个ContentProvider实现它的几个方法onCreate,query,getType,insert,delete,update
二、我们还需要一个dnhelper
2.新建一个Dbhelper
public class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context) { super(context, "person", null, 1); } @Override public void onCreate(SQLiteDatabase sqlitedatabase ) {sqlitedatabase.execSQL("create table person(_id integer primary key autoincrement,name,age)");sqlitedatabase.execSQL("insert into person values(null,'哈哈',21)");}
三、实例化dbhelper
四、对页面的数据进行增查删改的一个操作
public void mequery(View view){
uri = Uri.parse("content://com.example.android_sqlite.myProvider");
Cursor cursor=cr.query(uri,null,null,null,null);
while (cursor.moveToNext()){
int pid= cursor.getInt(cursor.getColumnIndex("_id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
int pass= cursor.getInt(cursor.getColumnIndex("pass"));
Log.i("test",pid+""+name+""+pass);
}
}
public void meinserter(View view){
String name=et_main_name.getText().toString();
int pass=Integer.parseInt(et_main_pass.getText().toString());
uri=Uri.parse("content://com.example.android_sqlite.myProvider");
ContentValues values=new ContentValues();
values.put("name",name);
values.put("pass",pass);
values.putNull("_id");
cr.insert(uri,values);
}
public void meupdate(View view){
int myid=Integer.parseInt(et_main_id.getText().toString());
String name=et_main_myname.getText().toString();
int pass=Integer.parseInt(et_main_mypass.getText().toString());
uri=Uri.parse("content://com.example.android_sqlite.myProvider");
ContentValues values = new ContentValues();
values.put("name", name);
values.put("pass", pass);
String whereClause = "_id=?";
String[] whereArgs = new String[] { String.valueOf(myid) };
cr.update(uri,values, whereClause, whereArgs);
}
public void medelete(View view){
String name=et_main_deletename.getText().toString();
uri=Uri.parse("content://com.example.android_sqlite.myProvider");
cr.delete(uri,"name=?",new String[]{name});
}
}