Appending Data in BLOB fields

ckndr47ckndr47 Member Posts: 100
Hi Gurus,

I have a BLOB field where i am importing a txt file;
Next i have to create append mechansim that is when i select any other file, the system should not overwrite my existing data found BLOB field but it should append it.

Any quick trick to get this thing done apart from reading the blob field, createinstream and then read the data and thn put it somwhere.

I have done this copy stream trick but this isint working. Any trick or help

This code is called at the IMPORT button on Form:
Rec.CALCFIELDS(Comments);
IF Rec.Comments.HASVALUE THEN BEGIN
  RMCommentLine.Comments.IMPORT('*.txt',TRUE);
  RMCommentLine.Comments.CREATEINSTREAM(StreamInObj);
  Rec.Comments.CREATEOUTSTREAM(StreamOutObj);
  COPYSTREAM(StreamOutObj,StreamInObj);
  CurrForm.UPDATE;
END
ELSE BEGIN
  Rec.Comments.IMPORT('*.txt',TRUE);
  CurrForm.UPDATE;
END;

Answers

  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    I doubt you can append data to existing data in a BLOB-field.
    You'll have to export the contents of the BLOB-field to a file on disk, append the new data to this file and finally import this new file into the BLOB-field and thus overwriting the old data.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • todrotodro Member Posts: 117
    I doubt you can append data to existing data in a BLOB-field.
    You'll have to export the contents of the BLOB-field to a file on disk, append the new data to this file and finally import this new file into the BLOB-field and thus overwriting the old data.
    or instead of using a file, the blob can copied to another BLOB (with copystream as it is already done):

    1.) save the BLOB in on ther BLOB field (COYPSTREAM)
    2.) clear the original BLOB field
    3.) use a INSTREAM from the tempBLOB and wriite to the original field with WRITE
    4.) once INSTREAM (tempBLOB) is EOS, read from INSTREAM containing the data to append and use the same OUTSTREAM to write the data

    COPYSTREAM always seeks to position 0 within the OUTSTREAM. As you can not move the stream pointer, you can not simply use COPYSTREAM to append.
    Torsten
    MCP+I, MCSE NT, Navision MCT (2004,2005)
  • ckndr47ckndr47 Member Posts: 100
    Thanks todro and Luc Van for your valuable feedback.

    Yeah this thing can be done in 2 ways;
    a) either through exporting the files and merging them through Shell command
    b) Manipulation with streams.

    I have selected option b) and its working fine here at my end.

    Here is the code so that it can be useful for everybody:

    The code is written on the IMPORT button of form:
    IF NOT Rec.ISEMPTY THEN
      Rec.CALCFIELDS(Comments);
    
      IF Rec.Comments.HASVALUE THEN BEGIN // if data exists in BLOB
    
        RMCommentLine.Comments := Rec.Comments; // copy the BLOB field to temp1
    
        Rec.Comments.CREATEOUTSTREAM(StreamOutObj); 
    
       // Move data from Temp1 stream to the original BLOB field
        RMCommentLine.Comments.CREATEINSTREAM(StreamInObj);
        WHILE NOT (StreamInObj.EOS()) DO BEGIN
          StreamInObj.READTEXT(Txt);
          StreamOutObj.WRITE(COPYSTR(Txt,1,STRLEN(Txt)));
        END;
    
        // Get the new file (for the second file i m using a sep Rec)
        RMCommentLinetemp.Comments.IMPORT('*.txt',TRUE);
    
        // Append the data from Temp2 to original
        RMCommentLinetemp.Comments.CREATEINSTREAM(StreamInObj);
        WHILE NOT (StreamInObj.EOS()) DO BEGIN
          StreamInObj.READTEXT(Txt);
          StreamOutObj.WRITE(COPYSTR(Txt,1,STRLEN(Txt)));
        END;
    
        CurrForm.UPDATE;
      END
      ELSE BEGIN
        Rec.Comments.IMPORT('*.txt',TRUE);
        CurrForm.UPDATE;
      END;
    
    // Both Temp1 (RMCommentLine) and Temp2 (RMCommentLinetemp) are temporary tables
    
    Thanks and Regards,
    
Sign In or Register to comment.