android的内容提供者代码,android之ContentProvider内容提供者

package cn.itcast.db;

import android.content.ContentProvider;

import android.content.ContentUris;

import android.content.ContentValues;

import android.content.UriMatcher;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.net.Uri;

public class PersonProvider extends ContentProvider {

private DBOpenHelper dbOpenHelper;

private static final UriMatcher MATCHER=new

UriMatcher(UriMatcher.NO_MATCH);

private static final int PERSONS=1;

private static final int PERSON=2;

static{

MATCHER.addURI("com.yhj.provides.personprovider","person",PERSONS);

MATCHER.addURI("com.yhj.provides.personprovider","person/#",PERSON);

}

@Override

public boolean onCreate() {

dbOpenHelper=new DBOpenHelper(this.getContext());

return true;

}

@Override

public Cursor query(Uri uri, String[] projection, String

selection,

String[] selectionArgs, String sortOrder) {

SQLiteDatabase db=dbOpenHelper.getWritableDatabase();

switch (MATCHER.match(uri)) {

case 1:

return db.query("person", projection, selection,

selectionArgs, null, null, sortOrder);

case 2:

long rowid=ContentUris.parseId(uri);

String where ="personid=" + rowid;

if(selection !=null &&

!"".equals(selection.trim())){

where +="and" +selection;

}

return db.query("person", projection, where, selectionArgs,

null, null, sortOrder);

default:

throw new IllegalArgumentException("this is Unknown Uri:" +

uri);

}

}

@Override

public String getType(Uri uri) {

switch (MATCHER.match(uri)) {

case 1:

return "vnd.android.cursor.dir/person";

case 2:

return "vnd.android.cursor.item/person";

default:

throw new IllegalArgumentException("this is Unknown Uri:" +

uri);

}

}

@Override

public Uri insert(Uri uri, ContentValues values) {

SQLiteDatabase db=dbOpenHelper.getWritableDatabase();

switch (MATCHER.match(uri)) {

case 1:

long rowid= db.insert("person", "name", values);//主键值

//Uri

insertUri=Uri.parse("content://com.yhj.provides.personprovider/person/"

+ rowid);

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

return insertUri;

default:

throw new IllegalArgumentException("this is Unknown Uri:" +

uri);

}

}

@Override

public int delete(Uri uri, String selection, String[]

selectionArgs) {

SQLiteDatabase db=dbOpenHelper.getWritableDatabase();

int num=0;

switch (MATCHER.match(uri)) {

case 1:

num= db.delete("person",selection , selectionArgs);

break;

case 2:

long rowid=ContentUris.parseId(uri);

String where ="personid=" + rowid;

if(selection !=null &&

!"".equals(selection.trim())){

where +="and" +selection;

}

num=db.delete("person", where, selectionArgs);

break;

default:

throw new IllegalArgumentException("this is Unknown Uri:" +

uri);

}

return num;

}

@Override

public int update(Uri uri, ContentValues values, String

selection,

String[] selectionArgs) {

SQLiteDatabase db=dbOpenHelper.getWritableDatabase();

int num=0;

switch (MATCHER.match(uri)) {

case 1:

//num= db.delete("person",selection , selectionArgs);

num=db.update("person", values, selection,

selectionArgs);

break;

case 2:

long rowid=ContentUris.parseId(uri);

String where ="personid=" + rowid;

if(selection !=null &&

!"".equals(selection.trim())){

where +="and" +selection;

}

//num=db.delete("person", where, selectionArgs);

num=db.update("person", values, where, selectionArgs);

break;

default:

throw new IllegalArgumentException("this is Unknown Uri:" +

uri);

}

return num;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值