3

I'm trying to create a generic helper class for db4o.

I created a static class which should work as helper in each applications pages.

Here is my code:

public final class DatabaseHelper implements IDatabaseHelper{

    private static ObjectContainer database;
    private static final String DATABASE_NAME = "MyDB.db4o";
    private static final int DATABASE_MODE = 0;
    private static Context ctx;

    private DatabaseHelper(){
    }

    public DatabaseHelper(Context context) {
        ctx=context;
        database=null;
    }

    public void OpenDatabse(){

        try {
            if (database == null) {
                EmbeddedConfiguration nc = Db4oEmbedded.newConfiguration();
                database = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(),
                        db4oDBFullPath());
            }
        } catch (Exception ie) {
            Log.e(DatabaseHelper.class.getName(), ie.toString());
        }


    }

    private String db4oDBFullPath() {
        return ctx.getDir("data", DATABASE_MODE) + "/" + DATABASE_NAME;
    }

    public void CloseDatabase() {
        if (database != null) {
            database.close();
        }
    }

    public ObjectContainer getDatabaseSession() {

        return database.ext().openSession();
    }

}

My problem is in the OpenDatabase() method. I always got

com.db40.ext.Db4oException: File format incompatible: '/data/data/personal.pier.appname/app_data/MyDB.db4o'

I tried adding/removing the final attribute. I tried calling it step by step. I'm new to db4o and I don't really know how to do it.

More generally, is it possible to create a general helper class? Any help will be appreciated. Thanks

Jonnus
  • 2,988
  • 2
  • 24
  • 33
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66

1 Answers1

0

Yes, it's possible to create such a generic help class. The final attribute works fine with db4o. I also recommend to capture the whole stack trace for getting more out of the error:

Log.e(DatabaseHelper.class.getName(), ie.toString());

As the message states, the file format for some reason does not match. So the next questions are:

  • Does for some reason the file already exist by the time Db4oEmbedded.openFile() is called?
  • Is the database created on another platform, like C# and then copied over? That does not work. The Java and C# version cannot read each others database properly?
  • Do other file operations work on that android file? Like can read and write with a RandomAccessFile to that file location?
Gamlor
  • 12,978
  • 7
  • 43
  • 70