{"id":3260,"date":"2020-01-22T21:57:09","date_gmt":"2020-01-22T20:57:09","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3260"},"modified":"2020-01-22T21:57:09","modified_gmt":"2020-01-22T20:57:09","slug":"inserting-a-blob","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=3260","title":{"rendered":"Inserting a BLOB"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Inserting a BLOB in Oracle is slightly different from inserting from inserting a value in an Oracle database. We first have to create a location with n empty BLOB. After that the BLOB can be inserted. In a PL\/SQL programme:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ncreate or replace PROCEDURE UPLOAD_FILE_TO_DATABASE(p_dir_name varchar2,p_file_name varchar2,p_file_desc varchar2 default null)\nAS\n    L_BLOB  BLOB;\n    L_BFILE  BFILE := BFILENAME(p_dir_name, p_file_name);\nBEGIN\n    -- Insert a reacord with empty blog\n    INSERT INTO MY_FILE_TABLE(nummer,FILE_NAME, FILE_LOB,DESCRIPTION)\n        VALUES (file_no_seq.nextval,p_file_name, empty_blob(),p_file_desc)\n        RETURNING FILE_LOB INTO L_BLOB;-- Open the bfile locator \nDBMS_LOB.OPEN(L_BFILE, DBMS_LOB.LOB_READONLY);\n-- Open the blob locator to add the file \nDBMS_LOB.OPEN(L_BLOB, DBMS_LOB.LOB_READWRITE);\n-- Add the file to blob locator\nDBMS_LOB.LOADFROMFILE(DEST_LOB => L_BLOB,\n                      SRC_LOB  => L_BFILE,\n                      AMOUNT   => DBMS_LOB.GETLENGTH(L_BFILE));\n-- Close the lob locators\nDBMS_LOB.CLOSE(L_BLOB);\nDBMS_LOB.CLOSE(L_BFILE);\nCOMMIT;\nEND UPLOAD_FILE_TO_DATABASE;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">which call be invoked by (example) exec UPLOAD_FILE_TO_DATABASE(&#8216;TEMP&#8217;,&#8217;2019-11-15 12.46.30.mov&#8217;,&#8217;Nora&#8217;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Likewise, downloading a BLOB is done via:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">create or replace procedure download_file_from_database( p_file_number number)\nis\n    l_file           utl_file.file_type;    \n    l_buffer_data    raw(32767);            \n    l_byte_amount    binary_integer := 32767;  \n    l_position       integer        := 1;   \n    l_blob           blob;                 \n    l_blob_length    integer;              \n    l_file_name      varchar2(250);       \nbegin\n  -- fetch the blob content from table into variable\n  select file_lob,\n         file_name\n  into l_blob,\n       l_file_name\n  from hr.my_file_table\n  where nummer  = p_file_number;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">-- compute the length of blob content\n  l_blob_length   := dbms_lob.getlength(l_blob);\n  l_position      := 1;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">-- open the destination file.\n  l_file := utl_file.fopen('EXTERNORA',l_file_name,'wb', l_byte_amount); <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">-- read chunks of the blob and write them to the file<br>   -- until complete.<br>   while l_position &lt; l_blob_length<br>   loop<br>     dbms_lob.read(l_blob, l_byte_amount, l_position, l_buffer_data);<br>     utl_file.put_raw(l_file, l_buffer_data, true);<br>     l_position := l_position + l_byte_amount;<br>   end loop;  <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">-- close the file.<br>   utl_file.fclose(l_file); <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"> exception<br> when others then<br>   -- if anything goes wrong, then close the file.<br>   if utl_file.is_open(l_file) then<br>     utl_file.fclose(l_file);<br>   end if;<br>   raise;<br> end download_file_from_database; <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Al this download is invoked via: exec DOWNLOAD_FILE_FROM_DATABASE(5);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An alternative way is to store data directly into the database via:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">insert into my_file_table(FILE_NAME, FILE_LOB, DESCRIPTION, NUMMER) values ('prut', utl_raw.cast_to_raw('some magic here'), 'onzin',6);\n\nfollowed by\n\nexec DOWNLOAD_FILE_FROM_DATABASE(6);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The text &#8220;some magic here&#8221; is then shown as being inserted into file &#8220;prut&#8221; that is stored in the database.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inserting a BLOB in Oracle is slightly different from inserting from inserting a value in an Oracle database. We first have to create a location with n empty BLOB. After that the BLOB can be inserted. In a PL\/SQL programme: which call be invoked by (example) exec UPLOAD_FILE_TO_DATABASE(&#8216;TEMP&#8217;,&#8217;2019-11-15 12.46.30.mov&#8217;,&#8217;Nora&#8217;); Likewise, downloading a BLOB is done [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3260","post","type-post","status-publish","format-standard","hentry","category-nice-to-know"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3260"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3260\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3260"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}