Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
WesleyT
Posts: 206
Registered: ‎07-02-2010
My Device: 8320 Curve
My Carrier: Vodacom

Storing Data without signing solution

Right !

 

This has made me climb up the wall for two straight days. So i thought i could possibly save someone the frustration that i have had.

 

The following code will save a record to a RecordStore which will persist even when app has been resetted. You can manipulate this code to sute your needs.

 

Now this will save a record each time you obviously add it.

 

What i am now doing is just adding one record, and adding a string in xml format. Each time i want to add to the "database" i retrieve the string and run it in an xml reader (this will me an xml in memory), there i will manipulate the string and update the old record in the RecordStore.

 

I hope this helps some poor soul that struggles. Excuse my writing skills, i am not the best writer in the bunch.

 

 

import javax.microedition.rms.*;

import com.vlb.mainpages.UIvlbScreen;

public class ReadWrite extends UIvlbScreen
{
  private RecordStore rs = null;
  static final String REC_STORE = "db_1";

  public ReadWrite()
  {
      super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT);
    openRecStore();   // Create the record store
    
    // Write a few records and read them back
    writeRecord("Test 1");    
    readRecords();
    
    closeRecStore();  // Close record store
    deleteRecStore(); // Remove the record store
  }

  public void destroyApp( boolean unconditional )
  {
  }

  public void startApp()
  {
  }

  public void pauseApp()
  {
  }

  public void openRecStore()
  {
    try
    {
      // The second parameter indicates that the record store
      // should be created if it does not exist
      rs = RecordStore.openRecordStore(REC_STORE, true );
    }
    catch (Exception e)
    {
      db(e.toString());
    }
  }    
 
  public void closeRecStore()
  {
    try
    {
      rs.closeRecordStore();
    }
    catch (Exception e)
    {
      db(e.toString());
    }
  }

  public void deleteRecStore()
  {
    if (RecordStore.listRecordStores() != null)
    {
      try
      {
        RecordStore.deleteRecordStore(REC_STORE);
      }
      catch (Exception e)
      {
        db(e.toString());
      }
    }      
  }

  public void writeRecord(String str)
  {
    byte[] rec = str.getBytes();

    try
    {
      rs.addRecord(rec, 0, rec.length);
    }
    catch (Exception e)
    {
      db(e.toString());
    }
  }

  public void readRecords()
  {
    try
    {
      // Intentionally make this too small to test code below
      byte[] recData = new byte[5];
      int len;

      for (int i = 1; i <= rs.getNumRecords(); i++)      
      {
        if (rs.getRecordSize(i) > recData.length)
          recData = new byte[rs.getRecordSize(i)];
       
        len = rs.getRecord(i, recData, 0);
        System.out.println("Record #" + i + ": " + new String(recData, 0, len));
        System.out.println("------------------------------");                        
      }
    }
    catch (Exception e)
    {
      db(e.toString());
    }
  }

  /*--------------------------------------------------
  * Simple message to console for debug/errors
  * When used with Exceptions we should handle the
  * error in a more appropriate manner.
  *-------------------------------------------------*/
  private void db(String str)
  {
    System.err.println("Msg: " + str);
  }
}
Please use plain text.
Trusted Contributor
ABoul
Posts: 138
Registered: ‎07-11-2010
My Device: Bold 9700
My Carrier: O2

Re: Storing Data without signing solution

[ Edited ]

got it to work but it's throwing a SecurityException. no idea how to handle this.

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Storing Data without signing solution

A SecurityException when working with RecordStore generally means that your midlet/app is trying to access a record store that was created by another midlet suite in AUTHMODE_PRIVATE mode, or is trying to change the mode of a record store it does not own. Do you know what statement is throwing the SecurityException?




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Trusted Contributor
ABoul
Posts: 138
Registered: ‎07-11-2010
My Device: Bold 9700
My Carrier: O2

Re: Storing Data without signing solution

[ Edited ]

 


Ted_Hopp wrote:

A SecurityException when working with RecordStore generally means that your midlet/app is trying to access a record store that was created by another midlet suite in AUTHMODE_PRIVATE mode, or is trying to change the mode of a record store it does not own. Do you know what statement is throwing the SecurityException?


just changed the name and it worked. thanks for that. do you know by any chance where i'm going wrong with this?

 

 

        rw = new ReadWrite();
        rw.openRecStore();
        rw.writeRecord("hi");
        String data = rw.readRecords(); //*
        rw.closeRecStore();
        rw.deleteRecStore();

 

it doesn't seem to delete the record. i have modified the readRecords() function so that it returns a string with the records seperated by a delimeter.

 

 

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Storing Data without signing solution

Perhaps the underlying record store is still open. Calling closeRecordStore() does not actually close the store until it has been called as many times as openRecordStore() has been called. Are you getting a RecordStoreException when rw.deleteRecStore() is executed?




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Trusted Contributor
ABoul
Posts: 138
Registered: ‎07-11-2010
My Device: Bold 9700
My Carrier: O2

Re: Storing Data without signing solution

[ Edited ]

 


Ted_Hopp wrote:

Perhaps the underlying record store is still open. Calling closeRecordStore() does not actually close the store until it has been called as many times as openRecordStore() has been called. Are you getting a RecordStoreException when rw.deleteRecStore() is executed?


i can't see exceptions since System.err or System.out don't show on my simulator for some reason. however, i checked and openRecordStore() is only called once. ok it looks like i AM getting a RecordStoreException.

 

 

EDIT: got it to work. stupidly removed the "final static" part of the database name and forgot to mention it. thanks for your help.

Please use plain text.