0

I'm using db4o to store different objects in an android application. I get json contents from a sever then them into Objects (contacts here) that I store in a db4o file.

ObjectContainer mainDB = Db4oEmbedded.openFile(dbConfig(), DB_MAIN);
mainDB.store(aContact);

Now if I directly make a query to get them back from the DB, it works.

try {
ObjectSet<Contact> result = db.queryByExample(Contact.class);
contacts = new ArrayList<Contact>(result);
} catch (Exception e) {
e.printStackTrace();
}

I can loop on the arraylist and the contacts are there, I can print their names and other fiels. But now if I close de db :

mainDB.close();

If I reopen it with the same code, there's nothing inside anymore, contacts.size() returns 0. What's wrong? Any idea? Thanks in advance...

Alexis
  • 16,629
  • 17
  • 62
  • 107
  • Are you running in an emulator? What does DB_MAIN looks like? How does your configuration (dbConfig()) looks like?. This usually happens when db4o is able to find the database file and initializes a new one. – Vagaus Nov 23 '10 at 15:10
  • is that link related? http://developer.db4o.com/Forums/tabid/98/aft/10178/Default.aspx – Sebastian Roth Nov 23 '10 at 15:11
  • I run on both emulator and nexus one. I have this field for the location of the DB : public final static String DB_MAIN = Environment.getExternalStorageDirectory() + "/circusMain.db4o"; and here's the dbConfig() : – Alexis Nov 24 '10 at 06:53
  • and here's the dbConfig() : private EmbeddedConfiguration dbConfig() { EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration(); configuration.common().objectClass(Model.class).objectField("id").indexed(true); configuration.common().objectClass(Contact.class).objectField("name").indexed(true); //other objectField configuration for Tasks, Activities and Opportunities return configuration; } – Alexis Nov 24 '10 at 07:04
  • @ened: I don't use the TransparentPersistentSupport like it's done in that link. – Alexis Nov 24 '10 at 07:48

2 Answers2

1

If you try a SODA query rather than querybyexample it works?

German
  • 10,263
  • 4
  • 40
  • 56
0

What's the result of Environment.getExternalStorageState()? There might be a problem with the dir. What happens if you try Context.getExternalFilesDir() as directory or getExternalStoragePublicDirectory()?

What happens if you use this dir when opening the container?

ctx.getDir("data", 0) + "/" + "db.file";

where ctx is the app context.

German
  • 10,263
  • 4
  • 40
  • 56