Dbutils数据库增删改查

 1 package com.example.day5_xutildemo;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import com.baidu.vo.Car;
 7 import com.lidroid.xutils.DbUtils;
 8 import com.lidroid.xutils.db.sqlite.Selector;
 9 import com.lidroid.xutils.exception.DbException;
10 
11 import android.os.Bundle;
12 import android.app.Activity;
13 import android.view.Menu;
14 
15 public class MainActivity extends Activity {
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         
22         //通过xutils里的DButils类创建数据库
23         DbUtils db = DbUtils.create(this, "dbutils.db");
24         try {
25             db.createTableIfNotExist(Car.class);//创建小车表
26             
27             Car c1 = new Car("奔驰", 3.0f, "red");
28             db.save(c1);//保存对象
29             Car c2 = new Car("宝马", 3.0f, "red");
30             db.save(c2);//保存对象
31             
32             Car c = new Car(1,"奔驰", 10.0f, "red");
33             db.update(c, null);//db.update(c, new String[]{"price"});只修改价钱字段
34             
35             db.delete(c);
36             
37 //            ArrayList<Car> cars = (ArrayList<Car>) db.findAll(Car.class);//全查
38             List<Car> cars = db.findAll(Selector.from(Car.class).where("id", "<", 4));//过滤查询
39             System.out.println(cars);
40             
41         } catch (DbException e) {
42             // TODO Auto-generated catch block
43             e.printStackTrace();
44         }
45         
46     }

你可能感兴趣的:(Dbutils数据库增删改查)