The database is opened with "Open DB" button.
SQLite:
01public static SQLiteDatabase database(){ 02
long startTime = 0; 03
try { 04
_db = _context.openDatabase(DATABASE_NAME, null); 05
} catch (FileNotFoundException e) { 06
try { 07
_db = 08
_context.createDatabase(DATABASE_NAME, DATABASE_VERSION, 0, 09
null); 10
_db.execSQL("create table " + DB_TABLE_PILOT + " (" 11
+ "id integer primary key autoincrement, " 12
+ "name text not null, " 13
+ "points integer not null);"); 14
// Foreign key constraint is parsed but not enforced 15
// Here it is used for documentation purposes 16
_db.execSQL("create table " + DB_TABLE_CAR + " (" 17
+"id integer primary key autoincrement," + 18
"model text not null," + 19
"pilot integer not null," + 20
"FOREIGN KEY (pilot)" + 21
"REFERENCES pilot(id) on delete cascade);"); 22
_db.execSQL("CREATE INDEX CAR_PILOT ON " + DB_TABLE_CAR + " (pilot);"); 23
} catch (FileNotFoundException e1) { 24
_db = null; 25
} 26
} 27
logToConsole(startTime, "Database opened: ", false); 28
return _db; 29
}
db4o:
01public static ObjectContainer database(){ 02
long startTime = 0; 03
try { 04
if(_container == null){ 05
startTime = System.currentTimeMillis(); 06
_container = Db4o.openFile(configure(), db4oDBFullPath()); 07
} 08
} catch (Exception e) { 09
Log.e(Db4oExample.class.getName(), e.toString()); 10
return null; 11
} 12
logToConsole(startTime, "Database opened: ", false); 13
return _container; 14
}
1private static Configuration configure(){ 2
Configuration configuration = Db4o.newConfiguration(); 3
configuration.objectClass(Car.class).objectField("pilot").indexed(true); 4
configuration.objectClass(Pilot.class).objectField("points").indexed(true); 5
configuration.lockDatabaseFile(false); 6
7
return configuration; 8
}
db4o code is a bit more compact, but the main advantage of db4o is in the fact that all APIs are pure java, they are compile-time checked and can be transferred into IDE templates (database opening should be a template as it most probably be the same for all your db4o applications including tests).