In order to test the retrieval abilities of both databases we will try to select a car with a pilot having15 points:
SQLiteDatabase db = database(); Cursor cursor = db.rawQuery( "SELECT c.model, p.name, p.points, r.id, r.year" + " FROM " + DB_TABLE_CAR + " c, " + DB_TABLE_PILOT + " p " + "WHERE c.pilot = p.id AND p.points = ?;", new String[] { "15" }); cursor.moveToFirst(); Pilot pilot = new Pilot(); pilot.setName(cursor.getString(1)); pilot.setPoints(cursor.getInt(2)); Car car = new Car(); car.setModel(cursor.getString(0)); car.setPilot(pilot);
ObjectContainer db = database(); ObjectSet<Car> cars = db.query(new Predicate<Car>() { @Override public boolean match(Car car) { return car.getPilot().getPoints() == 15; } }); Car car = cars.get(0);
The db4o native queries are typesafe. This is a huge benefit, since the compiler can detect errors and the IDE help you with the refactoring. In the example above you can see that SQLite needs a lot of additional code to transfer the retrieved data into application's objects, whereas db4o does not need this code at all, as the result is already a collection of objects.