通過內容提供器介面可以向資料庫表中添加資料,但無法更新和洗掉,不知道哪里出錯了,求指教代碼如下package com.example.databasetest;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 DatabaseProvider extends ContentProvider { public static final int BOOK_DIR=0; public static final int BOOK_ITEM=1; public static final int CATEGORY_DIR=2; public static final int CATEGORY_ITEM=3; public static final String AUTHORITY="com.example.databasetest.provider"; private static UriMatcher uriMatcher; private MyDatabaseHelper dbHelper; static{ uriMatcher=new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(AUTHORITY,"book",BOOK_DIR); uriMatcher.addURI(AUTHORITY,"book/#",BOOK_ITEM); uriMatcher.addURI(AUTHORITY,"category",CATEGORY_DIR); uriMatcher.addURI(AUTHORITY,"category/#",CATEGORY_ITEM); } public DatabaseProvider() { } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { // Implement this to handle requests to delete one or more rows. SQLiteDatabase db=dbHelper.getWritableDatabase(); int deletedRows=0; switch(uriMatcher.match(uri)){ case BOOK_DIR: deletedRows=db.delete("Book",selection,selectionArgs); break; case BOOK_ITEM: String bookId=uri.getPathSegments().get(1); deletedRows=db.delete("Book","id=?",new String[]{bookId}); break; case CATEGORY_DIR: deletedRows=db.delete("Category",selection,selectionArgs); break; case CATEGORY_ITEM: String categoryId=uri.getPathSegments().get(1); deletedRows=db.delete("Category","id=?",new String[]{categoryId}); break; default: break; } return deletedRows; //throw new UnsupportedOperationException("Not yet implemented"); } @Override public String getType(Uri uri) { // TODO: Implement this to handle requests for the MIME type of the data // at the given URI. switch(uriMatcher.match(uri)){ case BOOK_DIR: return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.book"; case BOOK_ITEM: return "vnd.android.cursor.item/vnd.com.example.databasetest.provider.book"; case CATEGORY_DIR: return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.category"; case CATEGORY_ITEM: return "vnd.android.cursor.item/vnd.com.example.databasetest.provider.category"; } return null; //throw new UnsupportedOperationException("Not yet implemented"); } @Override public Uri insert(Uri uri, ContentValues values) { // TODO: Implement this to handle requests to insert a new row. SQLiteDatabase db=dbHelper.getWritableDatabase(); Uri uriReturn=null; switch(uriMatcher.match(uri)){ case BOOK_DIR: case BOOK_ITEM: long newBookId=db.insert("Book",null,values); uriReturn=Uri.parse("content://"+AUTHORITY+"/book"+newBookId); break; case CATEGORY_DIR: case CATEGORY_ITEM: long newCategoryId=db.insert("Category",null,values); uriReturn =Uri.parse("content//"+AUTHORITY+"/category"+newCategoryId); break; default: break; } return uriReturn; //throw new UnsupportedOperationException("Not yet implemented"); } @Override public boolean onCreate() { // TODO: Implement this to initialize your content provider on startup. dbHelper=new MyDatabaseHelper(getContext(),"BookStore.db",null,2); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO: Implement this to handle query requests from clients. SQLiteDatabase db=dbHelper.getReadableDatabase(); Cursor cursor=null; switch(uriMatcher.match(uri)){ case BOOK_DIR: cursor=db.query("Book",projection,selection,selectionArgs,null,null,sortOrder); break; case BOOK_ITEM: String bookId=uri.getPathSegments().get(1); cursor=db.query("Book",projection,"id=?",new String[]{bookId},null,null,sortOrder); break; case CATEGORY_DIR: cursor=db.query("Category",projection,selection,selectionArgs,null,null,sortOrder); break; case CATEGORY_ITEM: String categoryId=uri.getPathSegments().get(1); cursor=db.query("Category",projection,"id=?",new String[]{categoryId},null,null,sortOrder); break; default: break; } return cursor; //throw new UnsupportedOperationException("Not yet implemented"); } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO: Implement this to handle requests to update one or more rows. SQLiteDatabase db=dbHelper.getWritableDatabase(); int updatedRows=0; switch(uriMatcher.match(uri)){ case BOOK_DIR: updatedRows=db.update("Book",values,selection,selectionArgs); break; case BOOK_ITEM: String bookId=uri.getPathSegments().get(1); updatedRows=db.update("Book",values,"id=?",new String[]{bookId}); break; case CATEGORY_DIR: updatedRows=db.update("Category",values,selection,selectionArgs); break; case CATEGORY_ITEM: String categoryId=uri.getPathSegments().get(1); updatedRows=db.update("Category",values,"id=?",new String[]{categoryId}); break; default: break; } return updatedRows; //throw new UnsupportedOperationException("Not yet implemented")以上; }}是內容提供器的。package com.example.providertest;import android.content.ContentValues;import android.database.Cursor;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private String newId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button addData=https://bbs.csdn.net/topics/(Button)findViewById(R.id.add_data); addData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Uri uri=Uri.parse("content://com.example.databasetest.provider/book"); ContentValues values=new ContentValues(); values.put("name","A Clash Of Kings"); values.put("author","Georgr Martin"); values.put("pages",1040); values.put("price",22.85); Uri newUri=getContentResolver().insert(uri,values); newId=newUri.getPathSegments().get(1); } }); Button queryData=https://bbs.csdn.net/topics/(Button)findViewById(R.id.query_data); queryData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Uri uri=Uri.parse("content://com.example.databasetest.provider/book"); Cursor cursor=getContentResolver().query(uri,null,null,null,null); if(cursor!=null){ while(cursor.moveToNext()){ String name=cursor.getString(cursor.getColumnIndex("name")); String author=cursor.getString(cursor.getColumnIndex("author")); int pages=cursor.getInt(cursor.getColumnIndex("pages")); double price=cursor.getDouble(cursor.getColumnIndex("price")); Log.d("MainActivity","book name is"+name); Log.d("MainActivity","book author is"+author); Log.d("MainActivity","book pages is"+pages); Log.d("MainActivity","book price is"+price); } cursor.close(); } } }); Button updateData=https://bbs.csdn.net/topics/(Button)findViewById(R.id.update_data); updateData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Uri uri=Uri.parse("content://com.example.databasetest.provider/book/"+newId); ContentValues values=new ContentValues(); values.put("name","A Storm Of Swords"); values.put("pages",1216); values.put("price",24.05); getContentResolver().update(uri,values,null,null); } }); Button deleteData=https://bbs.csdn.net/topics/(Button)findViewById(R.id.delete_data); deleteData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Uri uri=Uri.parse("content://com.example.databasetest.provider/book/"+newId); getContentResolver().delete(uri,null,null); } }); }}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/123196.html
標籤:Android
上一篇:CoreBluetooth搜索藍牙設備,很多名稱為null,為什么設定里面的藍牙可以搜到名字?
下一篇:微信小游戲顯示不全問題
