问题
I wrote several files into one BLOB field using this code:
create or replace procedure blob_loader as
dir varchar2(50) := 'FILES_TO_LOAD';
lblob BLOB;
lfile BFILE;
f UTL_FILE.FILE_TYPE := UTL_FILE.FOPEN(dir, 'index.txt', 'R');
buffer VARCHAR2(30000);
begin
insert into blob_table
(blob_file)
values
(empty_blob())
returning blob_file into lblob;
dbms_lob.open(lblob, dbms_lob.lob_readwrite);
LOOP
BEGIN
utl_file.get_line(f, buffer);
lfile := BFILENAME(dir, buffer);
dbms_lob.open(lfile, dbms_lob.lob_readonly);
dbms_lob.loadfromfile(lblob, lfile, dbms_lob.getlength(lfile));
dbms_lob.close(lfile);
EXCEPTION
WHEN no_data_found THEN
EXIT;
END;
END LOOP;
dbms_lob.close(lblob);
commit;
end blob_loader;
Now I want to read the files from that field back to the disk, so that they were separate files again.
Does anyone have an idea how to define when one file in the blob field ends and another one starts?
Little help?
回答1:
Putting aside the obvious advice to just store each file as a separate BLOB ...
Seems to me you have two choices. Either store the length of each file somewhere, and use that to control the amount of data you read from the BLOB; or put some delimiter value into the BLOB between the files. The delimiter seems like worse choice to me - you have to pick a value that is guaranteed not to be present in the file data, and you have to read single bytes to find the delimiter.
So store the lengths. And probably the filenames. Which implies a child table with one row per file. Which brings us back to the question -- why not just store a BLOB in each of those rows, one per file?
来源:https://stackoverflow.com/questions/17427695/reading-multiple-values-from-a-blob-field-pl-sql