您好,登錄后才能下訂單哦!
一、在AndroidManifest.xml文件中添加
<provider android:name=".StudentProvider" android:authorities="com.example.android_contentprovider2.StudentProvider" > </provider>
二、創(chuàng)建數(shù)據(jù)庫SqliteOpenHelper
public class DbHelper extends SQLiteOpenHelper { private static String name = "mydb.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 database) { // TODO Auto-generated method stub String sql = "create table student (id integer primary key autoincrement ,name varchar(64) ,address varchar(64))"; database.execSQL(sql);// 對數(shù)據(jù)庫的表的創(chuàng)建 } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
三、創(chuàng)建ContentProvider
public class StudentProvider extends ContentProvider { private final String TAG = "StudentProvider"; private DbHelper helper = null; private static final UriMatcher URI_MATCHER = new UriMatcher( UriMatcher.NO_MATCH); private static final int STUDENT = 1;// 操作單條記錄 private static final int STUDENTS = 2;// 操作多條記錄 static { URI_MATCHER.addURI( "com.example.android_contentprovider2.StudentProvider", "student", STUDENTS); URI_MATCHER.addURI( "com.example.android_contentprovider2.StudentProvider", "student/#", STUDENT); } public StudentProvider() { // TODO Auto-generated constructor stub } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { // TODO Auto-generated method stub int count = -1;// 影響數(shù)據(jù)庫的行數(shù) try { int flag = URI_MATCHER.match(uri); SQLiteDatabase database = helper.getWritableDatabase(); switch (flag) { case STUDENT: // content://com.example.android_contentprovider2.StudentProvider/student/1 // delete from student where id = ? //id 通過客戶端傳遞過來的 long id = ContentUris.parseId(uri); String where_value = " id = " + id; if (selection != null && !selection.equals("")) { where_value += " and " + selection; } count = database.delete("student", where_value, selectionArgs); break; case STUDENTS: count = database.delete("student", selection, selectionArgs); break; } } catch (Exception e) { // TODO: handle exception } return count; } @Override public String getType(Uri uri) { // TODO Auto-generated method stub int flag = URI_MATCHER.match(uri); switch (flag) { 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 // insert into student () (?,?); Uri resultUri = null; int flag = URI_MATCHER.match(uri); switch (flag) { case STUDENTS: SQLiteDatabase database = helper.getWritableDatabase(); long id = database.insert("student", null, values);// 插入當(dāng)前行的行號 resultUri = ContentUris.withAppendedId(uri, id); break; } Log.i(TAG, "---->>" + resultUri.toString()); return resultUri; } @Override public boolean onCreate() { // TODO Auto-generated method stub helper = new DbHelper(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub Cursor cursor = null; try { SQLiteDatabase database = helper.getReadableDatabase(); int flag = URI_MATCHER.match(uri); switch (flag) { case STUDENT: long id = ContentUris.parseId(uri); String where_value = " id = " + id; if (selection != null && !selection.equals("")) { where_value += " and " + selection; } cursor = database.query("student", null, where_value, selectionArgs, null, null, null, null); break; case STUDENTS: cursor = database.query("student", null, selection, selectionArgs, null, null, null); break; } } catch (Exception e) { // TODO: handle exception } return cursor; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub int count = -1; try { // update table set name = ? ,address = ? where id = ? SQLiteDatabase database = helper.getWritableDatabase(); long id = ContentUris.parseId(uri); int flag = URI_MATCHER.match(uri); switch (flag) { case STUDENT: String where_value = " id = " + id; if (selection != null && !selection.equals("")) { where_value += " and " + selection; } count = database.update("student", values, where_value, selectionArgs); break; } } catch (Exception e) { // TODO: handle exception } return count; } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。