首页 > 代码库 > ContentProvider
ContentProvider
package com.android.provider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class HelloContentProvider extends ContentProvider{
private static final UriMatcher um=new UriMatcher(UriMatcher.NO_MATCH);
private SQLiteDatabase sqlLite;
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
um.addURI("com.android.provider","book",1);
sqlLite=SQLiteDatabase.openDatabase("/data/data/com.android.provider/tt", null,SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.CREATE_IF_NECESSARY);
sqlLite.execSQL("create table books([id] Integer primary key.autoincrement not null,[bookname] varchar(30) not null)");
ContentValues cv=new ContentValues();
cv.put("bookname", "android开发");
sqlLite.insert("books", null, cv);
return false;
}
@Override
public Cursor query(Uri uri, String[] arg1, String arg2, String[] arg3,
String arg4) {
// TODO Auto-generated method stub
if(um.match(uri)==1){
Cursor cursor=sqlLite.query("books", null, null, null, null, null, null);
return cursor;
}else{
return null;
}
}
@Override
public int update(Uri uri, ContentValues values, String arg2, String[] arg3) {
// TODO Auto-generated method stub
if(um.match(uri)==1){
int result=sqlLite.update("books", values, null, null);
}
return 0;
}
}
ContentProvider