Storing Data

"Store" button creates and stores 100 of car objects (each including a reference to Pilot object) to each database.

SQLite:

SqlExample.java: fillUpDB
01public static void fillUpDB() throws Exception { 02 close(); 03 _context.deleteDatabase(DATABASE_NAME); 04 SQLiteDatabase db = database(); 05 if (db != null){ 06 long startTime = System.currentTimeMillis(); 07 for (int i=0; i<100;i++){ 08 addCar(db,i); 09 } 10 logToConsole(startTime, "Stored 100 objects: ", false); 11 startTime = System.currentTimeMillis(); 12 } 13 }
SqlExample.java: addCar
01private static void addCar(SQLiteDatabase db, int number) 02 { 03 ContentValues initialValues = new ContentValues(); 04 05 initialValues.put("id", number); 06 initialValues.put("name", "Tester"); 07 initialValues.put("points", number); 08 db.insert(DB_TABLE_PILOT, null, initialValues); 09 10 initialValues = new ContentValues(); 11 12 initialValues.put("model", "BMW"); 13 initialValues.put("pilot", number); 14 db.insert(DB_TABLE_CAR, null, initialValues); 15 }

db4o:

Db4oExample.java: fillUpDB
01public static void fillUpDB() throws Exception { 02 close(); 03 new File(db4oDBFullPath()).delete(); 04 ObjectContainer container=database(); 05 if (container != null){ 06 long startTime = System.currentTimeMillis(); 07 for (int i=0; i<100;i++){ 08 addCar(container,i); 09 } 10 logToConsole(startTime, "Stored 100 objects: ", false); 11 startTime = System.currentTimeMillis(); 12 container.commit(); 13 logToConsole(startTime, "Committed: ", true); 14 } 15 }
Db4oExample.java: addCar
1private static void addCar(ObjectContainer container, int points) 2 { 3 Car car = new Car("BMW"); 4 car.setPilot(new Pilot("Tester", points)); 5 container.set(car); 6 }

You can see that db4o handles adding objects to the database in a much more elegant way - #set(object) method is enough. In SQLite case it is much more difficult as you mush store different objects into different tables. Some of the additional work that SQLite developer will have to do is not visible in this example, i.e: