您的位置:首页 > 其它

DbUtils

2015-12-06 11:28 344 查看
1. 创建bean 必须要有id/_id属性(作为表的主键)

2. 使用DbUtils 并建数据库

//
//public static DbUtils create(Context context, String dbName)
//
dbUtils = DbUtils.create(MainActivity.this, "Student.db");


3. 插入数据

dbUtils.save(new Student("新垣结衣", 27));//插入单条数据(放在try-cath中)
dbUtils.saveAll(list);//插入集合数据(放在try-cath中)


4. 查询数据

Student student = dbUtils.findFirst(Selector.from(Student.class).where("name","=","新垣结衣"));
//查询符合条件的第一条数据
//import com.lidroid.xutils.db.sqlite.Selector;


List<Student> list = dbUtils.findAll(Selector.from(Student.class)
.where("id" ,"<", 54)
.and(WhereBuilder.b("age", ">", 20).or("age", " < ", 30))
.orderBy("id")
.limit(pageSize)
.offset(pageSize * pageIndex));
//查询集合数据


5. 修改数据

try
{
List<Student> findAll = dbUtils.findAll(Selector.from(Student.class).limit(5));

Student student = findAll.get(0);
student.setAge("50");

dbUtils.update(student);

} catch (DbException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}


6. 删除数据

private void dbUtilsDelete()
{
// TODO Auto-generated method stub
try {
List<Student> findAll = dbUtils.findAll(Selector.from(Student.class));

//dbUtils.deleteAll(findAll);

Student student = findAll.get(0);
dbUtils.delete(student);
} catch (DbException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: