Home > General > Using db4o as Database for Android

Using db4o as Database for Android

Sick of object rational mapping with SQLite?

I read a long time ago about the object database db4o, which is very handy for storing objects instead of mapping the variables of objects to tables of a database. After some work with SQLite at Android it crossed my mind and I finally had enough time to give it a try. Normally you have to do the mapping by hand, means writing kind of DB manager which handles requests. These requests need to be coded in SQL which makes it very time-consuming. So you need to take care of all CREATE, INSERT, SELECT, UPDATE and DELETE operations by yourself.

The approach of db40 is to persist an entire object. To make clear how it works in practice I’m going to give some code snippets:

The class for the objects which should be persisted:

public class Person{
   //vars
   public String name;
   public int number;
   public String email;

   //empty constructor
   public Person(){}

   //constructor for a retrieve operation
   public Person(String name, int number, String email){
   this.name = name;
   this.number = number;
   this.email = email;
}

Create the DB:

   String dbPath = "/data/data/" + getPackageName() + "/database";
   ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);

Insert an object:

   //creates object
   Person person = new Person("Tom", "100", "Tom@db4o.com");
   //saves object
      try {
         db.store(person);
      } finally {
         db.close();
      }

Retrieve all objects:

   try {
      result = db.queryByExample(Person.class);

      while(result.hasNext()){
         Person person = (Person)result.next();
         Log.v("TAG", "Name: " + person.name + " Number: " + person.number + " eMail: " + person.email);
      }
   } finally {
      db.close();
   }

Retrieve all objects with the persom.name “Tom”:

   //Specifies the search pattern (null and 0 is wildcard)
   Person protoPerson = new Person("Tom", 0, null);

   try {
      result = db.queryByExample(protoPerson);

      while(result.hasNext()){
         Person person = (Person)result.next();
         Log.v("TAG", "Name: " + person.name + " Number: " + person.number + " eMail: " + person.email);
      }
   } finally {
      db.close();
   }

Do you see what my point is? Very straight forward to use db framework which has in accordance with this document even high performance. Further documentation on how to use db4o is available here. Source of a prototype test app maybe later here.

There is a small snag: It is under GPL or a commercial license.

Cheers mavi

Categories: General Tags: , ,
  1. 11/11/2011 at 13:52 | #1

    nice article mavi… very usefull !
    just to mention that this to work on Honeycomb the

    should be explicitly included in AndroidManifest.xml

    I mean: uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”

    • 16/11/2011 at 23:51 | #2

      Actually not. If you specify the database path “dbPath = “/data/data/” + getPackageName() + “/database”;” the database file is saved into the private directory of the app. Therefore you don’t need any permission.

  2. Lonny Barthel
    17/11/2011 at 17:15 | #3

    I’m often to blogging and i actually recognize your content. The article has really peaks my interest. I am going to bookmark your website and keep checking for new information.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.