0) {DemonstrateUtil.showToastResult(th。07|07,数据库的常用操作。" />

07|07,数据库的常用操作

0增加一条数据

//返回插入数据实体的id long insert = studentDao.insert(new Student() { { this.setHobby("写代码"); this.setName("王小明"); this.setNumber(1); this.setAge(22); } }); if (insert > 0) { DemonstrateUtil.showToastResult(this, "成功,实体id" + insert); } else { DemonstrateUtil.showToastResult(this, "失败,实体id" + insert); }

1删除一条数据
studentDao.deleteByKey(1l);

2修改一条数据
studentDao .update(new Student() { { setNumber(1); setName("王小明"); setId(1l); setHobby("吃饭睡觉敲码"); } }); DemonstrateUtil.showToastResult(this, "数据修改!");

3查询一条数据
Student student = studentDao.load(1l); if (null != student) { tvShow.setText(student.toString()); } else { tvShow.setText("查询的数据不存在!!"); DemonstrateUtil .showToastResult(MyApplication.getInstance(), "数据不存在哦!!"); }

4增加List集合数据
studentDao .insertInTx(new ArrayList() { { for (int i = 1; i <= 5; i++) { int num = i + 1; num++; add(new Student(null, "张非" + i, "吃饭" + i, num, 20)); } add(new Student(null, "李四", "吃饭", 10, 15)); add(new Student(null, "李四", "睡觉", 11, 16)); add(new Student(null, "李四", "敲码", 12, 17)); add(new Student(null, "李四", "爱说话", 13, 18)); } });

5查询所有的数据
List studentList = studentDao.queryBuilder().list(); tvShow.setText(studentList.toString()); for (Student student : studentList) {DemonstrateUtil .showLogResult(student.toString()); }

6查询指定字段的数据
List students = studentDao .queryBuilder() .where(StudentDao.Properties.Name.eq("张非3")) .list(); tvShow.setText(students.toString()); for (Student student : students) { DemonstrateUtil .showLogResult(student.toString()); }

7查询指定字段的数据降序排列
List students = studentDao .queryBuilder() .where(StudentDao.Properties.Name.eq("李四")) .orderDesc(StudentDao.Properties.Age) .list(); tvShow.setText(students.toString()); for (Student student : students) { //DemonstrateUtil.showLogResult(student.toString()); }

【07|07,数据库的常用操作】8查询指定字段的数据升序排列
List students = studentDao .queryBuilder() .where(StudentDao.Properties.Name.eq("李四")) .orderAsc(StudentDao.Properties.Age) .list(); tvShow.setText(students.toString()); for (Student stu : students) { DemonstrateUtil.showLogResult(stu.toString()); }

9组合查询数据
List students = studentDao .queryBuilder() .where(StudentDao.Properties.Name.eq("李四"), StudentDao.Properties.Age.le(17)) .list(); tvShow.setText(students.toString()); for (Student stu : students) { DemonstrateUtil.showLogResult(stu.toString()); }

10查询所有的数据,只取指定条数
List students = studentDao .queryBuilder() .limit(3) .list(); tvShow.setText(students.toString()); for (Student student : students) { DemonstrateUtil .showLogResult(student.toString()); }

11查询所有,取指定条数,跳过某条
List students = studentDao .queryBuilder() .limit(3) .offset(2) .list(); tvShow.setText(students.toString()); for (Student student : students) { DemonstrateUtil .showLogResult(student.toString()); }

12查询数据总条数
/*tvShow .setText("总条数:" + studentDao.queryBuilder() .list().size()); */ tvShow.setText("总条数:" + studentDao.count());

13修改指定字段的数据
Student student = studentDao. queryBuilder() .where(StudentDao.Properties.Name.eq("张非1")) .unique(); if (null != student) { student.setName("张三"); studentDao.update(student); DemonstrateUtil.showToastResult(this, "修改成功!"); Student student1 = studentDao .queryBuilder() .where(StudentDao.Properties.Name.eq("张三")) .unique(); DemonstrateUtil .showLogResult(student1.toString()); } else { DemonstrateUtil .showToastResult(this, "要修改的数据不存在!!"); }

14删除指定数据方式1
studentDao .queryBuilder() .where(StudentDao.Properties.Name.eq("张非2")) .buildDelete() .executeDeleteWithoutDetachingEntities(); DemonstrateUtil.showLogResult("删除成功!");

15删除指定数据方式2
Student student = studentDao. queryBuilder() .where(StudentDao.Properties.Name.eq("张非4")) .unique(); studentDao.delete(student); DemonstrateUtil.showLogResult("删除,张非4");

16删除全部数据
//删除全部数据! studentDao.deleteAll();

    推荐阅读