今天在学习SQLite数据库存储知识的时候,发现SQLiteDatabase对象可以先通过ContentValues对象进行数据的组装,然后利用SQLiteDatabase对象自身的insert()方法将ContentValues对象传入进行数据的存储。
那么ContentValues是什么呢?
ContentValues和Bundle类似,都是使用HashMap的泛型形式来进行数据存储的类。但是区别在于Bundle可以存储Object对象,而ContentValues只能存储基本类型。Bundle相当于HashMap<String, Object>(),ContentValues则相当于HashMap<String,基本类型数据>()。
ContentValues的常用方法:
//向集合中添加基本类型数据,Double、Integer、String、Boolean、Byte、Float类型以此类推
public void put(String key, Short value);
public void put(String key, Long value);
public void put(String key); //添加空值
//移除ContentValues中的所有数据
public void clear();
//向当前ContentValues中添加传入的ContentValues对象中的所有值
public void putAll(ContentValues other);
//移除键值为key的数据
public void remove(String key);
//返回ContentValues对象中的数据是否为空
public Boolean isEmpty();
//返回数据个数
public int size();
//返回键值的集合
public Set<String> SetKey();
//获取键值为key的数据并转换为Boolean类型返回,String、Integer、Float、Double等类型以此类推
public Boolean getAsBoolean(String key)