Android之ContentProvider

新建一個DbHelper類繼承SQLiteOpenHelper
package com.example.contentprovider;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DbHelper extends SQLiteOpenHelper {
	private static String name="student.db";
	private static int version=1;

	public DbHelper(Context context) {
		super(context, name, null, version);
		// TODO Auto-generated constructor stub
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql="create table student(id integer primary key autoincrement,name varchar(64),address varchar(64),sex varchar(8) )";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}


新建StudentProvider繼承ContentProvider

package com.example.contentprovider;

import java.util.Currency;

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;
import android.util.Log;

public class StudentProvider extends ContentProvider {
	private DbHelper dbHelper=null;
	private final String TAG="StudentProvider";
	private final static int STUDENT=1;//操作單條記錄
	private final static int STUDENTS=2;//操作多條記錄
	private final static UriMatcher uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);

	static{
		uriMatcher.addURI("com.example.contentprovider.StudentProvider", "student/#", STUDENT);
		uriMatcher.addURI("com.example.contentprovider.StudentProvider", "student", STUDENTS);
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		dbHelper=new DbHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {


		Log.i(TAG, "1");
		// TODO Auto-generated method stub
		int flg=uriMatcher.match(uri);
		int count=-1;
		SQLiteDatabase db=null;
		Cursor cursor=null;
		switch (flg) {
		case STUDENT:
			Log.i(TAG, "2");
			long id=ContentUris.parseId(uri);
			try {
				db=dbHelper.getWritableDatabase();
				String whereClause="id = "+id;
				if (selection!=null && !selection.equals("")) {
					whereClause+="and "+selection;
				}
				cursor=db.query("student", null, whereClause, selectionArgs, null, null, null);
				String name="";
				while (cursor.moveToNext()) {
					name=cursor.getString(cursor.getColumnIndex("name"));
				}
				Log.i(TAG, "name---->"+name);
			} catch (Exception e) {
				Log.i(TAG, "excepton-->"+e.getMessage());
				// TODO: handle exception
			}finally{
				if (db!=null) {
					db.close();
				}
			}
			break;

		case STUDENTS:
			Log.i(TAG, "3");
			db=dbHelper.getWritableDatabase();
			try {
				cursor=db.query("student", null, selection, selectionArgs, null, null, null);
				String name="";
				while (cursor.moveToNext()) {
					name=cursor.getString(cursor.getColumnIndex("name"));
					Log.i(TAG, "name---->"+name);
				}
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}finally{
				if (db!=null) {
					db.close();
				}
			}
			break;
		}
		return cursor;
	
	
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		int code=uriMatcher.match(uri);
		switch (code) {
		case STUDENT:
			return "vnd.android.cursor.item/student";

		case STUDENTS:
			return"vnd.android.cursor.dir/students";
		}
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		Uri requestUri=null;
		SQLiteDatabase db=dbHelper.getWritableDatabase();
		long id=db.insert("student", null, values);
		requestUri=ContentUris.withAppendedId(uri, id);
		Log.i(TAG, "--->"+requestUri);
		return requestUri;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		Log.i(TAG, "1");
		// TODO Auto-generated method stub
		int flg=uriMatcher.match(uri);
		int count=-1;
		SQLiteDatabase db=null;
		switch (flg) {
		case STUDENT:
			Log.i(TAG, "2");
			long id=ContentUris.parseId(uri);
			try {
				db=dbHelper.getWritableDatabase();
				String whereClause="id = "+id;
				if (selection!=null && !selection.equals("")) {
					whereClause+="and "+selection;
				}
				count=db.delete("student", whereClause, selectionArgs);
				Log.i(TAG, "count---->"+count);
			} catch (Exception e) {
				Log.i(TAG, "excepton-->"+e.getMessage());
				// TODO: handle exception
			}finally{
				if (db!=null) {
					db.close();
				}
			}
			break;

		case STUDENTS:
			Log.i(TAG, "3");
			db=dbHelper.getWritableDatabase();
			try {
				count=db.delete("student", selection, selectionArgs);
				Log.i(TAG, "count---->"+count);
			} catch (Exception e) {
				// TODO: handle exception
			}finally{
				if (db!=null) {
					db.close();
				}
			}
			break;
		}
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {

		Log.i(TAG, "1");
		// TODO Auto-generated method stub
		int flg=uriMatcher.match(uri);
		int count=-1;
		SQLiteDatabase db=null;
		switch (flg) {
		case STUDENT:
			Log.i(TAG, "2");
			long id=ContentUris.parseId(uri);
			try {
				db=dbHelper.getWritableDatabase();
				String whereClause="id = "+id;
				if (selection!=null && !selection.equals("")) {
					whereClause+="and "+selection;
				}
				count=db.update("student", values, whereClause, selectionArgs);
				Log.i(TAG, "count---->"+count);
			} catch (Exception e) {
				Log.i(TAG, "excepton-->"+e.getMessage());
				// TODO: handle exception
			}finally{
				if (db!=null) {
					db.close();
				}
			}
			break;

		case STUDENTS:
			Log.i(TAG, "3");
			db=dbHelper.getWritableDatabase();
			try {
				count=db.update("student", values, selection, selectionArgs);
				Log.i(TAG, "count---->"+count);
			} catch (Exception e) {
				// TODO: handle exception
			}finally{
				if (db!=null) {
					db.close();
				}
			}
			break;
		}
		return count;
	
	}

}

新建Test類

package com.example.contentprovider;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.test.AndroidTestCase;

public class Test extends AndroidTestCase {

	public Test() {
		// TODO Auto-generated constructor stub
	}
	public void insert(){
		//訪問內容提供者的步驟
		//1.需要一個內容解析者
		ContentResolver contentResolver=getContext().getContentResolver();
		Uri url=Uri.parse("content://com.example.contentprovider.StudentProvider/student");
		ContentValues values=new ContentValues();
		values.put("name", "小明");
		values.put("address", "廣州市天河區");
		values.put("sex", "男");
		contentResolver.insert(url, values);
	}
	
	public void query(){
		ContentResolver contentResolver=getContext().getContentResolver();
		Uri url=Uri.parse("content://com.example.contentprovider.StudentProvider/student");
		contentResolver.query(url, null, "id in(?,?)", new String []{"5","6"}, null);
	}
	
	public void update(){
		ContentResolver contentResolver=getContext().getContentResolver();
		Uri url=Uri.parse("content://com.example.contentprovider.StudentProvider/student");
		ContentValues values=new ContentValues();
		values.put("name", "小青");
		values.put("address", "廣州市天河區");
		values.put("sex", "男");
		contentResolver.update(url, values, "id in(?)",new String[]{"5"});
	}
	
	public void delete(){
		ContentResolver contentResolver=getContext().getContentResolver();
		Uri url=Uri.parse("content://com.example.contentprovider.StudentProvider/student");
		contentResolver.delete(url, "id=?", new String[]{"4"});
//		contentResolver.delete(url, null, null);
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章