ContentValue添加数据
/**
* @param model 数据模型
* @return 返回添加数据有木有成功
*/
public boolean addPersonData(PersonModel model)
{
//把数据添加到ContentValues
ContentValues values = new ContentValues();
values.put(VALUE_NAME,model.getName());
values.put(VALUE_AGE,model.getAge());
values.put(VALUE_ISBOY,model.getIsBoy());
values.put(VALUE_ADDRESS,model.getAddress());
values.put(VALUE_PIC,model.getPic());
//添加数据到数据库
long index = getWritableDatabase().insert(TABLE_NAME_PERSON,null,values);
//大于0表示添加成功
if(index > 0)
{
return true;
}else
{
return false;
}
}
sql语句添加数据
/**
* sql语句添加数据,比较麻烦
*/
public void addPersonDataSql(PersonModel model)
{
//格式: insert into 表名 (字段名,字段名,…)value('字段值','字段值','…')
//看着很多,其实就是这个 insert into person (name,age,isboy,address,pic) values('五天','3','0','上海市浦东新区x606','[B@5340395')
String insertSql = "insert into "+TABLE_NAME_PERSON+" ("+
VALUE_NAME + ","+
VALUE_AGE + ","+
VALUE_ISBOY + ","+
VALUE_ADDRESS + ","+
VALUE_PIC+")"+
" values"+"("+
"'"+model.getName()+"',"+
"'"+model.getAge()+"',"+
"'"+model.getIsBoy()+"',"+
"'"+model.getAddress()+"',"+
"'"+model.getPic()+"'"+
")";
Log.e(TAG, ""+insertSql);
getWritableDatabase().execSQL(insertSql);
}
sqlite也无非就创建、增、删、改、查、更新。