}
package veryedu.cls9.lession8.contentprovider;
public class BookDBMetaData {
public static final String AUTHORITY="veryedu.cls9.lession8.provider.mybookprovider";
public static final String DBNAME="book.db";
public static final int VERSION=1;
public static final String TBL_BOOK="tbl_book";
public static final class TblBookMetaDate
{
public static final String TBL_BOOK="tbl_book";
public static final String _ID="_id";
public static final String BOOK_NAME="bookname";
public static final String BOOK_PRICE="bookprice";
public static final String AUTHOR="author";
public static final String BOOKS_TYPE="vnd.android.cursor.dir/vnd.veryedu.books";
public static final String BOOK_TYPE="vnd.android.cursor.item/vnd.veryedu.books";
public static final String BOOK_URI="content://"+AUTHORITY+"/book";
}
}
package veryedu.cls9.lession8.contentprovider;
import org.apache.http.client.utils.URIUtils;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.text.TextUtils;
public class MyBookProvider extends ContentProvider {
static UriMatcher uriMatcher;
public static final int BOOKS_CODE=1;
public static final int BOOK_CODE=2;
MyBookDBHelper dbHelper;
SQLiteDatabase db=null;
static{
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
//当前的Provider可以接收如下的两个请求地址
//第一个请求地址编号是1 content://veryedu.cls9.lession8.provider.mybookprovider/book
//这种地址是想要获得多条图书的信息
uriMatcher.addURI(BookDBMetaData.AUTHORITY, "book", 1);
//第2个请求地址编号是2 content://veryedu.cls9.lession8.provider.mybookprovider/book/13
//想要对book表中id编号是13的记录做操作
uriMatcher.addURI(BookDBMetaData.AUTHORITY, "book/#", 2);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
String type="";
switch(uriMatcher.match(uri))
{
case BOOKS_CODE:
type=BookDBMetaData.TblBookMetaDate.BOOKS_TYPE;
break;
case BOOK_CODE:
type=BookDBMetaData.TblBookMetaDate.BOOK_TYPE;
break;
default:
throw new IllegalArgumentException("非法的URI地址");
}
return type;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
db=dbHelper.getWritableDatabase();
switch(uriMatcher.match(uri))
{
case BOOKS_CODE:
long id= db.insert(BookDBMetaData.TblBookMetaDate.TBL_BOOK,
BookDBMetaData.TblBookMetaDate.AUTHOR,
values) ;
//通知所有对当前数据感兴趣的Observer观察者数据已发生变动
this.getContext().getContentResolver().notifyChange(uri, null);
//拼接出新的URI地址并返回
String uriStr=BookDBMetaData.TblBookMetaDate.BOOK_URI+"/"+id;
uri=Uri.parse(uriStr);
break;
default :
throw new IllegalArgumentException();
}
return uri;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
dbHelper=new MyBookDBHelper(this.getContext(), BookDBMetaData.DBNAME, null, BookDBMetaData.VERSION);
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
db=dbHelper.getReadableDatabase();
Cursor c=null;
switch(uriMatcher.match(uri))
{
// 单条地址:content://xxxxx/book/5
case BOOK_CODE:
long id=ContentUris.parseId(uri);
String where=BookDBMetaData.TblBookMetaDate._ID +"="+id;
if(selection!=null && selectionArgs!=null)
{
where= where+" and "+selection;
}
c= db.query(BookDBMetaData.TBL_BOOK, projection, where, selectionArgs, null, null, sortOrder);
break;
// 多条地址:content://xxxxx/book/
case BOOKS_CODE:
c= db.query(BookDBMetaData.TBL_BOOK, projection, selection, selectionArgs, null, null, sortOrder);
break;
}
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
private class MyBookDBHelper extends SQLiteOpenHelper
{
public MyBookDBHelper(Context context, String name,
CursorFactory factory, int version) {
//version是我们应用程序的版本号
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
//当应用程序在手机中第一次执行时,会调用该方法创建数据库表
//以后的执行中就不会调用该方法了
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql="create table tbl_book (_id integer primary key autoincrement,bookname,bookprice,author)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 升级 先删除原有的老表 然后按照新的结构重新建表
db.execSQL("drop tbl_book");
onCreate(db) ;
}
}
}