android sqlite数据库 删除数据
- where后跟条件表达式 =,!=,>,<,>=,<=
- 多条件 and or
直接看代码
/**
* 方法删除数据库数据
*/
public void deletePersonData(PersonModel model)
{
//where后跟条件表达式 =,!=,>,<,>=,<=
//多条件 and or
//删除数据库里的model数据 因为_id具有唯一性。
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+"=?",new String[]{""+model.getId()});
//删除数据库里 _id = 1 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+"=?",new String[]{"1"});
//删除 age >= 18 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_AGE+">=?",new String[]{"18"});
//删除 id > 5 && age <= 18 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+">?"+" and "+VALUE_AGE +"<=?",new String[]{"5","18"});
//删除 id > 5 || age <= 18 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+">?"+" or "+VALUE_AGE +"<=?",new String[]{"5","18"});
//删除数据库里 _id != 1 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+"!=?",new String[]{"1"});
//删除所有 _id >= 7 的男生
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ISBOY+"=?"+" and "+VALUE_ID+">=?",new String[]{"1","7"});
//删除所有 _id >= 7 和 _id = 3 的数据
getWritableDatabase().delete(TABLE_NAME_PERSON,VALUE_ID+">=?"+" or "+VALUE_ID+"=?",new String[]{"7","3"});
}
/**
* sql删除数据库数据
*/
public void deletePersonDataSql(PersonModel model)
{
//条件表达式 =,!=,>,<,>=,<=
//语法格式 delete from 表名 where 字段 条件表达式 '值'
//语法示例 delete from person where _id='2'
//多条件 delete from person where _id>'10' and age>'100'
//多条件 delete from person where _id>'10' or _id<'5'
//删除数据库里的model数据 因为_id具有唯一性。
String sql1 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+"="+"'"+model.getId()+"'";
//删除数据库里 _id = 1 的数据
String sql2 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+"="+"'"+1+"'";
//删除 age >= 18 的数据
String sql3 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_AGE+">="+"'"+18+"'";
//删除 id > 5 && age <= 18 的数据
String sql4 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+">"+"'"+5+"'"+" and "+
VALUE_AGE+"<="+"'"+18+"'";
////删除 id > 5 || age <= 18 的数据
String sql5 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+">"+"'"+5+"'"+" or "+
VALUE_AGE+"<="+"'"+18+"'";
//删除数据库里 _id != 1 的数据
String sql6 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+"!="+"'"+1+"'";
//删除所有 _id >= 7 的男生
String sql7 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+">="+"'"+7+"'"+" and "+
VALUE_ISBOY +"="+"'"+1+"'";
//删除所有 _id >= 7 和 _id = 3 的数据
String sql8 = "delete from "+TABLE_NAME_PERSON+" where "+
VALUE_ID+">="+"'"+7+"'"+" and "+
VALUE_ID+"="+"'"+3+"'";
Log.e(TAG, ""+sql7 );
getWritableDatabase().execSQL(sql7);
}
sqlite也无非就创建、增、删、改、查、更新。