CLOB AND LONGCHAR

Hello Everyone, hope you all are doing well. :)

For understanding LONGCHAR and CLOB datatype i am trying one scenario where i want
to store large text file(more then 50kb) into a CLOB field of database table and
later retrieve that field and display in EDITOR.
Code:
/*Code that I am trying to fill CLOB field:*/

DEF  VAR  l-val  AS  CHAR             NO-UNDO .  /* Either we should take char or longchar here */

INPUT FROM VALUE("c:\rajat\dynq.txt").  /* FILE SIZE 40 KB */
REPEAT :
     IMPORT  UNFORMATTED l-val.
     CREATE temp. /*DB Table With big as CLOB FIELD*/
     DISPLAY l-val.
     temp.big = l-val.
END.

INPUT CLOSE.

--------------------------------------------------------------------------------------------------------------------------------

/* Code that I am trying to display values of CLOB field */

DEF  VAR  l-val1  AS  LONGCHAR             NO-UNDO.

FOR EACH temp NO-LOCK:
     l-val1 = temp.big.
     DISPLAY l-val1 VIEW-AS EDITOR LARGE INNER-LINES 300 INNER-CHARS 300
     WITH FRAME x1 WIDTH 320.
END.
--------------------------------------------------------------------------------------------------------------------------------
If LONGCHAR is used to manipulate the values of CLOB field then isn't it work as a pointer or something then why would we directly assign values to it(I am confused).

Thanks & Regards !!!!
 
Last edited by a moderator:

tamhas

ProgressTalk.com Sponsor
look up COPY-LOB .... it will simplify your life considerably. If you want to break up the file into lines so that it will fit in ordinary characters, it is still easier to copy-lob the file into a longchar and then use entry(n,lcFile,"~n") to read the separate lines, but if you want to treat the file as one entry, do so!
 
Hi Tamhas, Thanks for the quick reply.

Again i tried the code with COPY-LOB and without it :
Code:
/*------------------------------------------------------------------------------------------------------------------------------------*/
DEF VAR l-val  AS CHAR                   NO-UNDO.
DEF VAR l-val1 AS LONGCHAR      NO-UNDO.
DEF VAR l-rslt AS CHAR                   NO-UNDO.

INPUT FROM VALUE("c:\rajat\dynq.txt").
REPEAT:
   IMPORT  UNFORMATTED l-val.
   CREATE temp.  /*DB Table With big as CLOB FIELD*/
   temp.big = l-val.
END.
INPUT CLOSE.

COPY-LOB FROM temp.big TO l-val1.

l-rslt = ENTRY(5,l-val1,"~n").
DISPLAY l-rslt.

/* If i run this code then i got error message that "ENTRY 5 IS OUTSIDE THE RANGE OF THE LIST"." */

/*---------------------------------------------------------------------------------------------------------------------------------------*/
If i do somthing like that:
Code:
DEF VAR l-val  AS CHAR  NO-UNDO.
DEF VAR l-val1 AS LONGCHAR  NO-UNDO.
DEF VAR l-rslt AS CHAR  NO-UNDO.

INPUT FROM VALUE("c:\rajat\dynq.txt").
REPEAT:
   IMPORT  UNFORMATTED l-val.
   CREATE temp. /*DB Table With big as CLOB FIELD*/
   temp.big = l-val.
END.
INPUT CLOSE.

COPY-LOB FROM temp.big TO l-val1.

DISPLAY l-val1 VIEW-AS EDITOR LARGE INNER-LINES 300 INNER-CHARS 300
  WITH FRAME x1 WIDTH 320.

According to this code only one value is visible under EDITOR, Perhaps i am doing somthing wrong while creating the table. I could to something like COPY-LOB FROM LONGCHAR TO FILE "c:\rajat\abc.txt" but i want know the role of LONGCHAR in this picture.

Thanks & Regards!!
Rajat.
 
Last edited by a moderator:
Hi Cringer,

Apologies, but i didn't get completely that what do mean by CODE TAGS. Do i need to use
Code:
 before i start coding and
after code ends and shouldn't insert /****/(comments there). Could you please elaborate so that next time i will make sure to use proper code tags.

Thanks & Regards!!
Rajat.
 

Cringer

ProgressTalk.com Moderator
Staff member
I added them to your posts so if you hit edit you'll see what I mean. :)
 
Wow, I got it when i posted this thread then content between CODE tags shown as progress code. Next time i will make sure to use proper code tags!!!

Thanks for Informing !!
Rajat.
 

TomBascom

Curmudgeon
Your two examples seem to have very little to do with one another other than some common code that completely misses the point.

In the first one you complain that you get an error about the number of entries. Ok, does the input file have 5 or more lines in it? Hint: use num-entries() to find out. And regardless -- what is magic about entry #5 that makes you want to explicitly get that line? Why not line 3? What about line 97?

In the second it doesn't work the way you hope because you are only copying one line (one record) into the longchar.

You seem to be confused about the difference between a (temp-) table and a record in that table.

The whole IMPORT, build a temp-table and then COPY-LOB (one field that contains just one line of input text) is silly. Just do this:

Code:
define variable bigString as longchar no-undo.

copy-lob from file "c:\rajat\dynq.txt" to bigString.

DISPLAY
  bigString VIEW-AS EDITOR LARGE INNER-LINES 300 INNER-CHARS 300
  WITH
  FRAME x1
  WIDTH 320
.
 
Hi Tom, Thanks for the reply.

1. Yes input file does have more then five lines, i took a random number #5 for ENTRY function.
2. I tried your example but according to it we are coping the data from a file to LONGCHAR data type, then where CLOB database field comes into picture and according to your code does COPY-LOB treats whole file as a CLOB Object and copied that into LONGCHAR?

Initially i wanted to take values from text file and fill database field(CLOB) .Code that i am trying for that:

Code:
DEF  VAR  l-val  AS  CHAR             NO-UNDO .  

INPUT FROM VALUE("c:\rajat\dynq.txt").  /* FILE SIZE 40 KB */
REPEAT :
     IMPORT  UNFORMATTED l-val.
     CREATE temp. /*DB Table With big as CLOB FIELD*/
     DISPLAY l-val.
     temp.big = l-val.
END.

INPUT CLOSE.

And later trying to retrieve value of this field big :

Code:
DEF  VAR  l-val1  AS  LONGCHAR             NO-UNDO.

FOR EACH temp NO-LOCK:
     l-val1 = temp.big.
     DISPLAY 
          l-val1 VIEW-AS EDITOR LARGE INNER-LINES 300 INNER-CHARS 300
          WITH
          FRAME x1 
          WIDTH 320.
END.

Thanks & Regards!!
Rajat.
 

tamhas

ProgressTalk.com Sponsor
The point of copy-log is that it treats the whole file as one entity, not line by line. You can look at lines by entry(n, longchar, "~n"). For example, here is one recent use I am making of the approach:
Code:
  method public integer CountLines (
      ipchSourceFile as character
      ):
    define variable mlcFile as longchar no-undo.
    define variable minLineCount as integer no-undo.
   
    copy-lob from file ipchSourceFile to mlcFile.
    minLineCount = num-entries( mlcFile, "~n" ).
    return minLineCount.
  end method.
 

TomBascom

Curmudgeon
It is always helpful to read the documentation:

COPY-LOB statement

Copies large object data between BLOBs, CLOBs, MEMPTRs, and LONGCHARs. It also copies large object data to and from the file system, and converts large object data to or from a specified code page.

Note: You cannot copy large object data between BLOBs and CLOBs directly. However, you can copy a BLOB or CLOB to a MEMPTR or LONGCHAR (which converts the data) and then copy the MEMPTR or LONGCHAR to the CLOB or BLOB, respectively.

You are starting with data in a file.

You want to get it into a CLOB field.

The best way to get that data from the file into a CLOB field (or a longchar variable) is to use COPY-LOB.

If you want to use an existing CLOB field "temp.big" then:

Code:
create temp.
copy-lob from "filename.txt" to temp.big.

display
  temp.big view-as editor large inner-lines 10 inner-chars 60
.

The IMPORT statement with a loop is reading the file as ordinary text line by line. That isn't dealing with a LOB field at all. (There is a way to read a LOB field as part of a record if you just use the buffer name -- but that's not what you are doing.)
 
Hi Tom,

I tried the above code, but got an error: "INPUT/OUTPUT operation are not Allowed..". I read the documentation again and tried this
code and it ran successfully, Perhaps we are not able to display CLOB field directly?

Am i writing the correct code, please confirm?

Code:
DEF VAR l-reslt AS LONGCHAR NO-UNDO.

CREATE temp.
COPY-LOB FROM FILE "filename.txt" TO temp.big.
COPY-LOB FROM OBJECT temp.big TO l-reslt.
DISPLAY
   l-reslt VIEW-AS EDITOR LARGE INNER-LINES 100 INNER-CHARS 100
   WITH
   FRAME x1
   WIDTH 150.

Thanks & Regards!!
Rajat.
 
Top