Android之内容提供者,你确定不进来看一看???

一、   创建应用程序并且继承一个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});




    }









}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值