Link to home
Start Free TrialLog in
Avatar of MrTerrence
MrTerrence

asked on

Add a record in recordset doesnt append at the end of record, but replace deleted record.

Hi all,

Let's say i have three records in a record set: Record 1,2,3. When i deleted record 2, now there is: Record 1,3. When i add a new record again, it shows that the new record is added as record 4,
I suppose the sequence will be 1,3,4. But when i recreate a new recordset, create a recordenumeration and try to read from the beginning..what i read out is record 1,4,3. :O!? Looks like the record 4 is not appended at the end of the recordset but replaced the deleted record?

I guess somehow the recordset pointer is still pointing at the location od the deleted record?
my code is quite standard, i always open a recordstore and close it after use. like this:
 
 
when deleting record 2 (simplified..)
========================
recordStore = RecordStore.openRecordStore(recordStoreName,true)
recordStore.deleteRecord(2);
recordStore.closeRecordStore();
 
when inserting new record
===================
recordStore = RecordStore.openRecordStore(recordStoreName,true)
byte[] b = .........;
int ID = recordStore.addRecord(b, 0, b.length); // this ID shows the record added is record 4 recordStore.closeRecordStore();
 
When enumerating again
=================
db2 =RecordStore.openRecordStore(picRsName,true);
RecordEnumeration recordEnumeration = db2.enumerateRecords(null,null,false);
while(recordEnumeration.hasNextElement()){
    int id = recordEnumeration.nextRecordId();
    System.out.println("reading >> record ID:" + id); //******************Over here, the output is 1, 4,3!
    ByteArrayInputStream bais = new ByteArrayInputStream(db2.getRecord(id));
    ...............reading bais...
}
 
db2.closeRecordStore();


===
p/s: To ensure addRecord() really add at the new record AFTER the last record, i even tried to go through a nextRecord() loop and makesure the pointer is pointing at last record before i call addRecord(), but the sequence read out is still wrong.
Avatar of gnoon
gnoon
Flag of Thailand image

Pls show the second sql statement. Is the second recordset sorted by id?
Actually, no matter what data sequence stored in the db, but how to get it in your sequence by programming.
Is the second sql statement look like this?

SELECT * FROM TABLE ORDER BY ID

'ORDER BY ID' will sort the recordset by 'ID' field ascendently.
Avatar of MrTerrence
MrTerrence

ASKER

Hi Gnoon,

I m not sure did u misunderstood anything, hmm but I m working on a J2ME mobile application. The recordset i mention is within the Record Management System (RMS)...so i didnt use any SQL in sorting actually.

Actually i didnt sort at all when i try to read out the records again, as shown in part 3 of the code, i just open the recordset, create an enumeration, then loop through each record. i thought this will give me the record one by one in the sequence how i added them.

any clues?

Sorry man, maybe I cant help you because I' not familiar with J2ME :-/ .
ASKER CERTIFIED SOLUTION
Avatar of edwardiii
edwardiii

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Though i didnt get a confirmative explaination on the scenario above (and i cant find any documentation abt it), but both the methods mentioned are workable. Thank you.