Opening the database is very similar.
Opening a SQLite database is very easy. However is necessary to generate the schema for the database.
SQLiteDatabase db = _context.openOrCreateDatabase(DATABASE_NAME,
Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS " + DB_TABLE_PILOT + " (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, points INTEGER NOT NULL);"); // Foreign key constraint is parsed but not enforced // Here it is used for documentation purposes db.execSQL("CREATE TABLE IF NOT EXISTS " + DB_TABLE_CAR + " (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "model TEXT NOT NULL, pilot INTEGER NOT NULL," + "FOREIGN KEY (pilot)" + "REFERENCES pilot(id) ON DELETE CASCADE);"); db.execSQL("CREATE INDEX IF NOT EXISTS CAR_PILOT ON " + DB_TABLE_CAR + " (pilot);");
Opening a db4o database is easy. First you need to create a file which is in the context of the application. Then you can open the database. To be faster, we configure additional indexes.
String filePath = context.getFilesDir() + "/android.db4o";
ObjectContainer db = Db4oEmbedded.openFile(configure(), filePath);
EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration(); configuration.common().add(new AndroidSupport()); configuration.common().objectClass(Car.class).objectField("pilot").indexed(true); configuration.common().objectClass(Pilot.class).objectField("points").indexed(true);
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).