2017-04-30 39 views
0

我使用的數據庫操作的內容提供商,都插入數據庫中的記錄成功地使用下面的代碼更新單列

 ContentValues messageContentValues = new ContentValues (); 

     // time stamp of message 
     messageContentValues.put (Conversation.COLUMN_TIME_STAMP, HeyUtil.getCurrentTime ()); 
     // date stamp of message 
     messageContentValues.put (Conversation.COLUMN_DATE_STAMP, HeyUtil.getCurrentDate ()); 


     messageContentValues.put (Conversation.COLUMN_MEDIA_NAME, fileName); 

      // Content providers Entry 
     Uri uri = getContentResolver ().insert (
       MyContentProviders.CONVERSATION_CONTENT_URI, messageContentValues); 

成功插入給我URI

現在我的問題是我可以使用這個URI來更新記錄嗎?

例如

// creat a content value for column you want to update 
         ContentValues mContentValues = new ContentValues(); 
         mContentValues.put (Conversation.COLUMN_MEDIA_NAME, "abde"); 


    // uri= record insertion URI 
      getContentResolver().update (uri,mContentValues,null,null); 
+0

您是否編寫了「ContentProvider」?它是否從'insert()'返回一個有效的'Uri'?它的'update()'方法是如何處理'Uri'的? –

+0

@MikeM。是的,所有在那裏 –

+2

那麼,是的,當然。只要'Uri'指向一個唯一的記錄,並且你的'update()'方法知道如何處理它。 –

回答

0

現在我的問題是我可以用這個URI來更新記錄?

正如Mike M.所表明的,這取決於您。如果您編寫ContentProvider,則可以根據需要實施update()方法。如果您想從Uri拉取ID值並使用它來更新單個條目,歡迎您這樣做。