标签:
1. 创建bean 必须要有id/_id属性(作为表的主键)
2. 使用DbUtils 并建数据库
1 // 2 //public static DbUtils create(Context context, String dbName) 3 // 4 dbUtils = DbUtils.create(MainActivity.this, "Student.db");
3. 插入数据
1 dbUtils.save(new Student("新垣结衣", 27));//插入单条数据(放在try-cath中) 2 dbUtils.saveAll(list);//插入集合数据(放在try-cath中)
4. 查询数据
Student student = dbUtils.findFirst(Selector.from(Student.class).where("name","=","新垣结衣")); //查询符合条件的第一条数据 //import com.lidroid.xutils.db.sqlite.Selector;
1 List<Student> list = dbUtils.findAll(Selector.from(Student.class) 2 .where("id" ,"<", 54) 3 .and(WhereBuilder.b("age", ">", 20).or("age", " < ", 30)) 4 .orderBy("id") 5 .limit(pageSize) 6 .offset(pageSize * pageIndex)); 7 //查询集合数据
5. 修改数据
1 try 2 { 3 List<Student> findAll = dbUtils.findAll(Selector.from(Student.class).limit(5)); 4 5 Student student = findAll.get(0); 6 student.setAge("50"); 7 8 dbUtils.update(student); 9 10 } catch (DbException e) 11 { 12 // TODO Auto-generated catch block 13 e.printStackTrace(); 14 }
6. 删除数据
1 private void dbUtilsDelete() 2 { 3 // TODO Auto-generated method stub 4 try { 5 List<Student> findAll = dbUtils.findAll(Selector.from(Student.class)); 6 7 //dbUtils.deleteAll(findAll); 8 9 Student student = findAll.get(0); 10 dbUtils.delete(student); 11 } catch (DbException e) 12 { 13 // TODO Auto-generated catch block 14 e.printStackTrace(); 15 } 16 17 }
标签:
原文地址:http://www.cnblogs.com/starman/p/5023222.html