Edgar
Edgar

Reputation: 1120

Add images to Oracle Database

Hello I am trying to add images to table. I made table:

    CREATE TABLE program_images
 (
 image_id NUMBER,
 image_filename VARCHAR2(50),
 image BLOB
 );

Then made directory where All images are: enter image description here

Finaly made procedure, which insert images to table:

create or replace 
PROCEDURE insert_image_file (p_id NUMBER, p_image_name IN VARCHAR2)
IS
   src_file   BFILE;
   dst_file   BLOB;
   lgh_file   BINARY_INTEGER;
BEGIN

  src_file := BFILENAME ('image_DIR', p_image_name);
   -- insert a NULL record to lock
   INSERT INTO program_images
               (image_id, image_filename, image
               )
        VALUES (p_id, p_image_name, EMPTY_BLOB ()
               )
     RETURNING image
          INTO dst_file;

   -- lock record
   SELECT     image
         INTO dst_file
         FROM program_images
        WHERE image_id = p_id AND image_filename = p_image_name
   FOR UPDATE;

   -- open the file
   DBMS_LOB.fileopen (src_file, DBMS_LOB.file_readonly);
   -- determine length
   lgh_file := DBMS_LOB.getlength (src_file);
   -- read the file
   DBMS_LOB.loadfromfile (dst_file, src_file, lgh_file);

   -- update the blob field
   UPDATE program_images
      SET image = dst_file
    WHERE image_id = p_id AND image_filename = p_image_name;

   -- close file
   DBMS_LOB.fileclose (src_file);
END insert_image_file;

Is that when I call procedure: EXECUTE insert_image_file(1,'audi_logo.png'); it says me that "non-existent directory or file for FILEOPEN operation" in procedure what is " DBMS_LOB.fileopen (src_file, DBMS_LOB.file_readonly);" this line. It was first time I use directories, so maybe I forget something to do?

Upvotes: 0

Views: 436

Answers (1)

The image directory name should be all UPPER_CASE, i.e.

src_file := BFILENAME ('IMAGE_DIR', p_image_name);

Per the docs for BFILENAME:

The directory argument is case sensitive. You must ensure that you specify the
directory object name exactly as it exists in the data dictionary. 

Upvotes: 3

Related Questions