package com.example.day5_xutildemo;
import java.util.ArrayList; import java.util.List;
import com.baidu.vo.Car; import com.lidroid.xutils.DbUtils; import com.lidroid.xutils.db.sqlite.Selector; import com.lidroid.xutils.exception.DbException;
import android.os.Bundle; import android.app.Activity; import android.view.Menu;
public class MainActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //通过xutils里的DButils类创建数据库 DbUtils db = DbUtils.create(this, "dbutils.db"); try { db.createTableIfNotExist(Car.class);//创建小车表 Car c1 = new Car("奔驰", 3.0f, "red"); db.save(c1);//保存对象 Car c2 = new Car("宝马", 3.0f, "red"); db.save(c2);//保存对象 Car c = new Car(1,"奔驰", 10.0f, "red"); db.update(c, null);//db.update(c, new String[]{"price"});只修改价钱字段 db.delete(c); // ArrayList<Car> cars = (ArrayList<Car>) db.findAll(Car.class);//全查 List<Car> cars = db.findAll(Selector.from(Car.class).where("id", "<", 4));//过滤查询 System.out.println(cars); } catch (DbException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
|