1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

MLE-API Tasks - Store information in the database

From mle

Jump to: navigation, search


Storing persistence information on the device:

Well don't expect a full-featured relational-database ;-)
There are actually two ways of storing information on the phone:

  • You can use the database feature of the MLE
  • You can store your data in the file-system of the device (use the methods of the "FactoryMedia"-class)

If you want to use the file-system storing capabilities, be aware that your phone must have the FILEIO-J2ME-API. If your phone doesn't have this API you can access the file-system. That's way we recommend to use the database-feature of the MLE, because it works in anycase.

The MLE database feature:

There are multiple ways to get a database:

  • FactoryForGeneralPhase.getApplicationDatabase() --> a system-wide database, accessible at everytime, you have to manage (close and delete) it on your own. Store application settings here.
  • FactoryForEventPhase.getContentDatabase() --> if the current document (pagesuite) is either already or is about to be stored on the local device. Store data here, that is related to the document (XML document). The framework manages this database. If the user deletes the document, this database will be deleted too.
  • FactoryForEventPhase.getTemporarayContentDatabase() --> If there is no content-database, but you need document-related database, use this temporary content databas. Store data here, that is related to the document (XML document). The framework manages this database. If the user closes this document, this database will be deleted.

You can create multiple application databases, but you have to delete them manually, if they are not needed anymore. The content database is always related to the document and that is way there is only one document database. But it is managed by the framework.

Here are two simple examples which should make everything clear:

Example: Storing application wide data (system settings):

public void informApplicationWasStarted(InfoApplication infoApp, boolean firstStart, boolean updated,ProgressBarPlugin bar) {
    try {
        Database db = FactoryForGeneralPhase.getApplicationDatabase("root", PLUGIN_ID, true);
        db.set("counter", COUNTER+"");
    } catch (Exception e) {
        FactoryForGeneralPhase.setErrorAlert("Some error occured while trying to store something on the device!",e,null);

In this example we just want to take count how often the user has started the application. So we load an application database called "root", which should be created if it doesn't exist. Than we load the database-value for the entry called "counter", increment it and store it back to the database. Afterwards we make a simply System.out to see the value, which increases every application start-up. Don't forget to close the database, if you don't need it anymore!

Example: Storing document related data (game score):

int score=112;
    // getting the content-database
    Database db = FactoryForEventPhase.getContentDatabase(gc.event.infoEvent, HelperEliberaPlugin.PLUGIN_ID);
    // if the content-database doesn't exist, we use the temporary content-database
    if(db==null)db=FactoryForEventPhase.getTemporarayContentDatabase(gc.event.infoEvent, HelperEliberaPlugin.PLUGIN_ID);
        int previousScore=HelperStd.parseInt(db.getString("score"), 0);
        db.set("score", ""+score);
}catch(Exception e){

In this example we want to store the high-score for a game. Because the game-data is related to a content-object (a XML document) the score should be stored with the game. So if the user deletes the content-object, the score and all other related game data should be deleted as well. That's why we store the data in the content-database.
At first we try to get the content-database. If there doesn't exist one, than the content-object (the XML document) wasn't stored on the device now. In this case we use the temporary database instead. Afterwards we retrieve the old score, set the new score and close the database.

As you can see handling persistence data is very easy with the MLE, far easier than the RecordStore Management System (RMS) of J2ME.

In the examples project of the MLE you can find a working example (jump & run game) for storing data in the database too.

Personal tools