本帖最后由 老衲玩IT 于 2013-8-25 21:36 编辑
db4o是面向对象的数据库,在build path菜单中添加db4o-8.1.184.15492-core-java5.jar,具体的初始化和增删改查请看如下代码
public class MainActivity extends Activity {
ObjectContainer db=Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), "sdcard/db4o.data");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 插入一条记录
* @param id
* @param name
* @param grade
*/
private void insert(int id,String name,float grade) {
Student student=new Student(id, name, grade);
db.store(student);
db.commit();
}
/**
* 按ID修改记录内容
* @param id
* @param name
* @param grade
*/
private void update(int id,String name,float grade) {
ObjectSet<Student> result=db.queryByExample(new Student(0, "huajun", 0));
if(result.hasNext()){
Student student=result.next();
if (student.getId()==id) {
student.setName(name);
db.store(student);
db.commit();
}
}
}
/**
* 查询所有
*/
private void query() {
ObjectSet<Student> result=db.queryByExample(new Student());
String s="";
while (result.hasNext()) {
Student student=result.next();
s+="["+student.getName()+":"+student.getGrade()+"]\n";
}
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
/**
* 按关键字删除
* @param id
* @return
*/
private int delete(int id) {
ObjectSet<Student> result=db.queryByExample(new Student());
//int deleted=0;
while (result.hasNext()) {
Student student=result.next();
if(student.getId()==id){
db.delete(student);
db.commit();
return 1;
}
}
return 0;
}
/**
* 按名字删除
* @param name
* @return
*/
private int delete(String name) {
ObjectSet<Student> result=db.queryByExample(new Student());
int deleted=0;
while (result.hasNext()) {
Student student=result.next();
if(student.getName().equals(name)){
db.delete(student);
deleted++;
db.commit();
}
}
return deleted;
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.query:
query();
break;
case R.id.update:
ObjectSet<Student> result=db.queryByExample(new Student(0, "huajun", 0));
if(result.hasNext()){
Student student=result.next();
student.setName("huajun");
db.store(student);
db.commit();
}
query();
break;
case R.id.delete:
ObjectSet<Student> result1=db.queryByExample(new Student(0, null, 0));
while(result1.hasNext()){
Student student=result1.next();
db.delete(student);
db.commit();
}
query();
break;
case R.id.insert:
insert((int) System.currentTimeMillis(),"学生"+System.currentTimeMillis(),new Random().nextInt(100));
query();
break;
}
return super.onOptionsItemSelected(item);
}
public class Student {
private int id;
private String name;
private float grade;
public Student(int id, String name, float grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public Student() {
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getGrade() {
return grade;
}
public void setGrade(float grade) {
this.grade = grade;
}
}
|
|