xUtils-DbUitls的使用

  1 public class MainActivity extends Activity {
  2 
  3     private DbUtils mDbUtils;
  4 
  5     private TextView mTv;
  6 
  7     @Override
  8     protected void onCreate(Bundle savedInstanceState) {
  9         super.onCreate(savedInstanceState);
 10         setContentView(R.layout.activity_main);
 11 
 12         mTv = (TextView) findViewById(R.id.tv);
 13 
 14         mDbUtils = mDbUtils.create(this);
 15         // mDbUtils = mDbUtils.create(this, "dbName.db");
 16         // 第二个参数指定数据库存放的位置
 17         // mDbUtils = mDbUtils.create(context, dbDir, dbName)
 18         // mDbUtils = mDbUtils.create(this, "dbName.db", 2, new
 19         // DbUpgradeListener() {
 20         //
 21         // @Override
 22         // public void onUpgrade(DbUtils arg0, int arg1, int arg2) {
 23         //
 24         // }
 25         // });
 26     }
 27 
 28     public void update(View view) {
 29         Person p = new Person("小红1", 11, "nv");
 30         try {
 31             // 更新和所给对象主键相同的数据,具体要更新哪咧的数据,由第二个可变长度数组指定,传null则所有列都更新
 32             // mDbUtils.update(p, null);
 33 
 34             // 将符合where条件的行,改为所给的Person的值
 35             mDbUtils.update(p, WhereBuilder.b("name", "=", "小李"), "sex", "age", "name");
 36 
 37         } catch (DbException e) {
 38             e.printStackTrace();
 39         }
 40     }
 41 
 42     /**
 43      * 数据查询
 44      */
 45     public void find(View view) {
 46         try {
 47             // 查找表中的所有数据
 48             List<Person> list = mDbUtils.findAll(Person.class);
 49 
 50             // 自定义条件 查找
 51             // List<Person> list =
 52             // mDbUtils.findAll(Selector.from(Person.class).where("sex", "=",
 53             // "女")
 54             // .and("age", "=", 15));
 55 
 56             // 数据查找排序
 57             // List<Person> list =
 58             // mDbUtils.findAll(Selector.from(Person.class).orderBy("age",
 59             // false));
 60 
 61             // Person p =
 62             // mDbUtils.findFirst(Selector.from(Person.class).where("sex", "=",
 63             // "男"));
 64 
 65             mTv.setText(list.toString());
 66         } catch (DbException e) {
 67             e.printStackTrace();
 68         }
 69     }
 70 
 71     /**
 72      * 删除
 73      */
 74     public void delete(View view) {
 75         // Person p = new Person("张三", 15, "斯蒂芬");
 76         try {
 77             // 删除所给的数据对应的表中,主键相同的数据(删除是不管除了主键外的其他数据是否相同)
 78             // mDbUtils.delete(p);
 79 
 80             mDbUtils.delete(Person.class,
 81                     WhereBuilder.b("sex", "=", "").and("age", "=", 18).or("age", "=", 32));
 82 
 83             // 删除所给的类的表中的所有数据
 84             // mDbUtils.deleteAll(Person.class);
 85 
 86             // 删除跟所给的list的类主键的值相同的数据
 87             // Person p1 = new Person("小明", 18, "男");
 88             // Person p2 = new Person("小红", 15, "女");
 89             // List<Person> list = new ArrayList<Person>();
 90             // list.add(p1);
 91             // list.add(p2);
 92             // mDbUtils.deleteAll(list);
 93 
 94         } catch (DbException e) {
 95             e.printStackTrace();
 96         }
 97     }
 98 
 99     /**
100      * 数据的插入 
101      */
102     public void insert(View view) {
103         Person p1 = new Person("小明", 18, "");
104          Child c = new Child("小强", "", 10);
105         // p1.mChild = c;
106 
107         // Person p2 = new Person("小红", 15, "女");
108         // Person p3 = new Person("小丽", 21, "女");
109         // Person p4 = new Person("小李", 32, "男");
110         //
111         // List<Person> list = new ArrayList<Person>();
112         // list.add(p1);
113         // list.add(p2);
114         // list.add(p3);
115         // list.add(p4);
116  
117         try {
118             // 将数据保存到数据库
119 //            mDbUtils.save(p1);
120             // 保存一个列表,将列表的每一个item作为数据库中的一行
121             // mDbUtils.saveAll(list);
122 
123             // 若数据库中没有存在 对应主键的数据,则保存数据
124             // 若已经存在对应主键的数据,则更新该主键对应的数据
125              mDbUtils.saveOrUpdate(p1);
126              mDbUtils.saveOrUpdate(c);
127             // mDbUtils.saveOrUpdateAll(list);
128         } catch (DbException e) {
129             e.printStackTrace();
130         }
131     }
132 }
 1 // 指定该类保存在数据库中的表名
 2 @Table(name = "Person")
 3 public class Person {
 4 
 5     // 默认情况下id作为xUtil数据库的主键,而且自增长的
 6     // public int id;
 7 
 8     @Id
 9     public String name;
10 
11     public int age;
12 
13     public String sex;
14 
15     // @Foreign(foreign = "id")
16     // public Child mChild;
17 
18     /**
19      * 若是重写了构造方法的话,必须提供一个不带 参数的构造方法,(查询的时候才会用到)
20      */
21     public Person() {
22 
23     }
24 
25     public Person(String name, int age, String sex) {
26         this.name = name;
27         this.age = age;
28         this.sex = sex;
29     }
30 
31     @Override
32     public String toString() {
33         return "Person [name=" + name + ", age=" + age + ", sex=" + sex + ", mChild=" + "]\n";
34     }
35 }
 1 public class Child {
 2 
 3     @Id
 4     public String name;
 5 
 6     public String sex;
 7 
 8     public int age;
 9 
10     public Child() {
11 
12     }
13 
14     public Child(String name, String sex, int age) {
15         this.name = name;
16         this.sex = sex;
17         this.age = age;
18     }
19 
20     @Override
21     public String toString() {
22         return "Child [ name=" + name + ", sex=" + sex + ", age=" + age + "]";
23     }
24 }

 

posted on 2015-04-08 17:54  CodeUtils  阅读(3799)  评论(0编辑  收藏  举报

导航