Xutils 中 对数据库的操作 DButils

public class MainActivity extends Activity {
	@ViewInject(R.id.creat)
	Button creat ;
	@ViewInject(R.id.add)
	Button add;
	@ViewInject(R.id.delete)
	Button delete;
	@ViewInject(R.id.quray)
	Button quray;
	@ViewInject(R.id.change)
	Button change;
	private DbUtils db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ViewUtils.inject(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@OnClick(R.id.creat)
	private void creat(View v){	
		db = DbUtils.create(MainActivity.this, "myStudy.db");
	}

	@OnClick(R.id.add)
	private void add(View v){
		if(db==null){
			db = DbUtils.create(MainActivity.this,"myStudy.db");
		}
		for(int i = 0;i<5;i++){
			Parent parent = new Parent();
			parent.setWord("王三"+i);
			parent.setEmail("wkjjfi@qq.com"+i);
			try {
				db.save(parent);
			} catch (DbException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	@OnClick(R.id.delete)
	private void delete(View v){
		if(db==null){
			db = DbUtils.create(MainActivity.this,"myStudy.db");
		}
		try {
			db.deleteById(Parent.class, 1);
		} catch (DbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	@OnClick(R.id.quray)
	private void quray(View v){	
		//查
		if(db==null){
			db = DbUtils.create(MainActivity.this,"myStudy.db");
		}
		try {
			List<Parent> parents = db.findAll(Parent.class);
			if(parents!=null){
				for(int i=0;i<parents.size();i++){
					LogUtils.e("数据为"+" "+parents.get(i).toString());
				}}
			else{
				LogUtils.e("读出数据为空");
			}
		} catch (DbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			LogUtils.e(db.findById(Parent.class, 23).toString());
		} catch (DbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@OnClick(R.id.change)
	private void change(View v){
		
		try {
			Parent parent = new Parent();
			parent.setWord("666232");
			parent.setEmail("32332323");
			db.update(parent, WhereBuilder.b("id","=",50),"word","email");
			//"update students set student_name=?,score=?,class_id=?  where student_id=?"		
			//db.execQuery("UPDATE Parent SET word = "+"4545"+ "WHERE id = 50");
		} catch (DbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值