ContentValues(Java)
生活随笔
收集整理的這篇文章主要介紹了
ContentValues(Java)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在SDK中,ContentValues的介紹為:
This class is used to store a set of values that the ContentResolver can process.
就是用于保存一些數據(string/boolean/byte/double/float/int/long/short …)信息,這些信息可以被數據庫操作時方便地使用。
ContentValues 和 HashTable 類似,都是一種存儲的機制,但是兩者最大的區別就在于:ContentValues 只能存儲基本類型的數據,像string、int之類的,不能存儲對象這種東西,而HashTable卻可以存儲對象。
一些常用的方法如下:
- ContentValues() Creates an empty set of values using the default initial size
- ContentValues(int size) Creates an empty set of values using the given initial size
- ContentValues(ContentValues from) Creates a set of values copied from the given set
比如向 SQLite 數據庫中插入數據的時候,首先應該有一個ContentValues的對象:
ContentValues cv = new ContentValues(); //創建對象 cv.put(key,values); // 賦值 SQLiteDataBase sdb ; // 創建數據庫 sdb.insert(database_name,null,initialValues); // 將數據插入數據庫- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
插入成功就返回記錄的id,否則返回-1.
說明:ContentValues是基于HashMap的,這與HashTable是不同的。
下面簡要說一下HashMap與HashTable的區別:
- Hashtable 繼承自 Dictiionary 而 HashMap繼承自AbstractMap。
- Hashtable的方法是Synchronize的,而HashMap不是,在多個線程訪問Hashtable時,不需要自己為它的方法實現同步,而HashMap 就必須為之提供外同步。
- Hashtable中,key和value都不允許出現null值。在HashMap中,null可以作為鍵,這樣的鍵只有一個;可以有一個或多個鍵所對應的值為null。當get()方法返回null值時,即可以表示 HashMap中沒有該鍵,也可以表示該鍵所對應的值為null。因此,在HashMap中不能由get()方法來判斷HashMap中是否存在某個鍵, 而應該用containsKey()方法來判斷。
總結
以上是生活随笔為你收集整理的ContentValues(Java)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 几种排序算法的思想
- 下一篇: ContentValues和HashTa