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();
}
}
}
Xutils 中 对数据库的操作 DButils
最新推荐文章于 2021-12-18 13:32:35 发布