Inserting Blob Data
From InterBase
Go Up to Accessing Blob Data with SQL
The following program inserts Blob data into the GUIDEBOOK column of the TOURISM table:
- Declare host-language variables to store the Blob ID, Blob segment data, and the length of the segment data:
EXEC SQL BEGIN DECLARE SECTION; BASED ON TOURISM.GUIDEBOOK blob_id; BASED ON TOURISM.GUIDEBOOK.SEGMENT blob_segment_buf; BASED ON TOURISM.STATE state; unsigned short blob_seg_len; EXEC SQL END DECLARE SECTION;
- The
BASED ON … SEGMENTsyntax declares a host-language variable,blob_segment_buf, that is large enough to hold a Blob segment during aFETCHoperation. For more information about theBASED ONdirective, see the Language Reference Guide.
- The
- Declare a Blob insert cursor:
EXEC SQL DECLARE BC CURSOR FOR INSERT Blob GUIDEBOOK INTO TOURISM;
- Open the Blob insert cursor and specify the host variable in which to store the Blob ID:
EXEC SQL OPEN BC INTO :blob_id;
- Store the segment data in the segment buffer,
blob_segment_buf, calculate the length of the segment data, and use anINSERT CURSORstatement to write the segment:sprintf(blob_segment_buf, 'Exploring Napa County back roads'); blob_segment_len = strlen(blob_segment_buf); EXEC SQL INSERT CURSOR BC VALUES (:blob_segment_buf:blob_segment_len);
Repeat these steps in a loop until you have written all Blob segments. - Close the Blob insert cursor:
EXEC SQL CLOSE BC;
- Use an
INSERTstatement to insert a new row containing the Blob into theTOURISMtable:EXEC SQL INSERT INTO TOURISM (STATE,GUIDEBOOK) VALUES ('CA',:blob_id); - Commit the changes to the database:
EXEC SQL COMMIT;