×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    record store fault

    What's wrong with this code?!?
    It should just write a record to RMS and read it back... but it does not! I write 1234, I read back 82537492!!

    So I examined Outputrecord and byteInputdata: they look like this!!

    Outputrecord =
    0
    0
    4
    -46

    byteInputData =
    49
    50
    51
    52

    ?????????


    Code:
      private void StoreSettings() {
        getTxtInfo().setString(getTxtInfo().getString()+"\n" + "Storing " + Integer.toString(TimeFixMs) );
        
        byte[] outputRecord; // Global array which collects settings.
        
        // Setup a stream to print data to:
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        DataOutputStream outputDataStream =  new DataOutputStream(outputStream);
        
        try {      
                outputDataStream.writeInt(TimeFixMs);   // Print data to stream.
                outputDataStream.flush();  // Committ changes.
                outputRecord = outputStream.toByteArray(); // Put stream into array for storing.
                RecordStore rs = RecordStore.openRecordStore("my_rms", true);
                outputStream.reset();   
                outputStream.close();
                outputDataStream.close();
                if (rs != null)
                {
                    rs.addRecord(outputRecord, 0, outputRecord.length); // Store array
                    rs.closeRecordStore();
                }
                System.out.println("Outputrecord = \n");            
                for (int i=0; i<outputRecord.length; i++) {
                    System.out.println(outputRecord[i]);
                }
                getTxtInfo().setString(getTxtInfo().getString()+"\n" + "Stored.");
              } 
        catch (RecordStoreFullException ex) {
            ex.printStackTrace();
        } catch (RecordStoreNotFoundException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
      }
      
      private void ReadSettings() {     
        TimeFixMs = 0;
        
        byte[] byteInputData = new byte[MILLI_LENGTH]; // Setup array to hold data to read.
        
        // Setup stream for reading data:
        ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
        DataInputStream inputDataStream = new DataInputStream(inputStream);  
        
        try { 
            RecordStore rs = RecordStore.openRecordStore("my_rms", true); // Open store.
            if (rs != null)
            {
                    rs.getRecord(1, byteInputData, 0); // Read all data into array.
                System.out.println("byteInputData = \n");            
                for (int i=0; i<byteInputData.length; i++) {
                    System.out.println(byteInputData[i]);
                }
                    
                    TimeFixMs = inputDataStream.readInt(); // Extract integer from array.
                    inputStream.reset();
                    inputStream.close();
                    inputDataStream.close();
                    System.out.println("Letto: ");
                    System.out.println(TimeFixMs);
                    rs.closeRecordStore();
            }
        }   catch (RecordStoreException e) {
                
        }    
        catch (Exception ex) {
            ex.printStackTrace();
        }
      }

  2. #2
    Nokia Developer Expert
    Join Date
    Jun 2005
    Posts
    923

    Re: record store fault

    If you ran this code twice, the record ID got incremented, therefore ID = 1 does not match the data you have written previously. You are adding N records, but reading only the one in position 1...

    Also, no need to use "flush(), reset()" or defining the size of the array in the "readSettings()" method. Close the streams only when you're finished with them; best place is the finally {} clause.

    Daniel

  3. #3
    Super Contributor
    Join Date
    Mar 2003
    Posts
    580

    Re: record store fault

    Quote Originally Posted by dcrocha View Post
    If you ran this code twice, the record ID got incremented, therefore ID = 1 does not match the data you have written previously. You are adding N records, but reading only the one in position 1...

    Also, no need to use "flush(), reset()" or defining the size of the array in the "readSettings()" method. Close the streams only when you're finished with them; best place is the finally {} clause.

    Daniel
    ops, you're right.... I obtained the two methods by "splitting" a sample source I found... And I forgot adding same header/footer to the 2 new methods.

    I fixed the code and did some tests: first execution using addRecord(), next ones using setRecord().... but it looks like it's not yet working, at least on emulator (BTW, I can't understand why after deleting the whole recordstore it still appears containing 11 records!! I had to use a different name to create a new recordstore!).

    But I need to do further tests.

Similar Threads

  1. Record Management Store issue
    By kartikm0021 in forum Mobile Java General
    Replies: 7
    Last Post: 2008-06-10, 16:41
  2. Register application: record store alternate
    By tgkprog@gmail.com in forum Mobile Java General
    Replies: 1
    Last Post: 2007-09-07, 08:21
  3. Record Store vs JDBC. How to store data?
    By 180ºrevolution in forum Mobile Java General
    Replies: 0
    Last Post: 2004-09-17, 15:24
  4. Record Store problem
    By anandmanish in forum Mobile Java General
    Replies: 3
    Last Post: 2003-09-15, 10:30
  5. Using protocol file:// with object connector
    By afinistr in forum Mobile Java General
    Replies: 2
    Last Post: 2002-06-24, 07:12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •