当前位置:操作系统 > 安卓/Android >>

android中数据存储的contentprovider的使用方法

元数据接口
 
package com.example.contentproviderprojecrt;
import android.net.Uri;
import android.provider.BaseColumns;
public inte易做图ce MLDNbatabaseMetaData {
//外部访问,content地址://com.example.contentproviderproject
public  static final String AUTHORITY="com.example.contentproviderproject";
//数据库的名称
public static final String DATABASE_NAME="mldn.db";
//数据库的版本
public static final int VERSION=1;
//member表的元素定义,直接继承与_ID和_COUNT静态变量
public static inte易做图ce MemberTableMetaData extends BaseColumns{
//数据表的名称
public static final String TABLE_NAME="member";
//外部访问的URI地址
public static final Uri CONTENT_URI=Uri.parse("content://"+
AUTHORITY+"/"+TABLE_NAME);
//取得member表中的所有数据
public static final String CONTACT_LIST="vnd.android.cursor.dir/vnd.contentproviderproject.member";
//取得一个member信息
public static final String CONTACT_ITEM="vnd.android.cursor.item/vnd.contentproviderproject.member";
//字段名称
public static final String MEMBER_NAME="name";
public static final String MEMBER_AGE="age";
public static final String MEMBER_BIRTHDAY="birthday";
//显示时候的排序字段
public static final String SORT_ORDER="_id DESC";
}
}
 
定义数据库操作类助手
 
package com.example.contentproviderprojecrt;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabaseHelper extends SQLiteOpenHelper {
public MyDatabaseHelper(Context context) {
//创建数据库
super(context, MLDNbatabaseMetaData.DATABASE_NAME, null, MLDNbatabaseMetaData.VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建表
String sql="CREATE TABLE "+MLDNbatabaseMetaData.MemberTableMetaData.TABLE_NAME+" ("
+MLDNbatabaseMetaData.MemberTableMetaData._ID+" INTEGER  PRIMARY KEY,"
+MLDNbatabaseMetaData.MemberTableMetaData.MEMBER_NAME+" VARCHAR(50)  NOT NULL,"
+MLDNbatabaseMetaData.MemberTableMetaData.MEMBER_AGE+"  INTEGER  NOT NULL,"
+MLDNbatabaseMetaData.MemberTableMetaData.MEMBER_BIRTHDAY+"   DATE   NOT NULL"
+")";
db.execSQL(sql);//执行SQL语句
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//更新表
String sql="DROP TABLE IF EXISTS "+MLDNbatabaseMetaData.MemberTableMetaData.TABLE_NAME;
db.execSQL(sql);//执行SQL语句
this.onCreate(db);//更新数据库
}
}
 
 
定义表操作contentProvider类
 
package com.example.contentproviderprojecrt;
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.net.Uri;
public class MemberContentProvider extends ContentProvider {
private static UriMatcher uriMatcher=null;   //定义UriMatcher对象
private static final int GET_MEMBER_LIST=1;   //查询全部的常量标记
private static final int GET_MEMBER_ITEM=2;   //根据ID查询的常量标记
private MyDatabaseHelper helper=null;      //数据库操作类对象
static{
uriMatcher=new UriMatcher(uriMatcher.NO_MATCH);  //实例化UriMatcher
uriMatcher.addURI(MLDNbatabaseMetaData.AUTHORITY, 
MLDNbatabaseMetaData.MemberTableMetaData.TABLE_NAME, GET_MEMBER_LIST);//增加匹配项
uriMatcher.addURI(MLDNbatabaseMetaData.AUTHORITY,
MLDNbatabaseMetaData.MemberTableMetaData.TABLE_NAME+"/#", GET_MEMBER_ITEM);//增加匹配项
}
//删除数据操作
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db=this.helper.getWritableDatabase(); //以写的方式打开
int result=0;//操作结果
switch(uriMatcher.match(uri)){//匹配传入的Uri
case GET_MEMBER_LIST:
result=db.delete(MLDNbatabaseMetaData.MemberTableMetaData.TABLE_NAME, selection, selectionArgs);
break;
case GET_MEMBER_ITEM:
long id=ContentUris.parseId(uri);
String where="_id="+id;
result=db.delete(MLDNbatabaseMetaData.MemberTableMetaData.TABLE_NAME, where, selectionArgs);
break;
default:
throw new UnsupportedOperationException("NOT SUPPORT OPERATION "+uri);
}
return result;
}
@Override
public String getType(Uri uri) {
switch(uriMatcher.match(uri)){//匹配传入的Uri
case GET_MEMBER_LIST:
return MLDNbatabaseMetaData.MemberTableMetaData.CONTACT_LIST;
case GET_MEMBER_ITEM:
return MLDNbatabaseMetaData.MemberTableMetaData.CONTACT_ITEM;
default:
throw new UnsupportedOperationException("NOT SUPPORT OPERATION "+uri);//抛出异常
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db=this.helper.getWritableDatabase();//以写的方式打开
long id=0;   //增加之后的ID
switch(uriMatcher.match(uri)){//匹配传入的Uri
case GET_MEMBER_LIST:
id=db.insert(MLDNbatabaseMetaData.MemberTableMetaData.TABLE_NAME,
MLDNbatabaseMetaData.MemberTableMetaData._ID, values);
String uriPath=uri.toString();//取出地址
String path=uriPath+"/"+id;   //建立新的URI地址
return Uri.parse(path);
case GET_MEMBER_ITEM:
return null;
default:
throw new UnsupportedOperationException("NOT SUPPORT OPERATION "+uri);
}
}
@Override
public boolean onCreate() {
this.helper=new MyDatabaseHelper(super.getContext());  //实例化DatabaseHelper
return true;  //操作成功
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db=this.helper.getReadableDatabase();//以读的方式打开
switch(uriMatcher.match(uri)){   //匹配传入的Uri
case GET_MEMBER_LIST:
return db.query(MLDNbatabaseMetaData.MemberTableMetaData.TABLE_NAME,
projection, selection, selectionArgs, null, null, sortOrder);
case GET_MEMBER_ITEM:
long id=ContentUris.parseId(uri);
String where="_id="+id;
return db.query(MLDNbatabaseMetaData.MemberTableMetaData.TABLE_NAME, projection, where, selectionArgs, null, null, sortOrder);
default:
throw new UnsupportedOperationException("NOT SUPPORT OPERATION "+uri);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selec
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,