ORACLE大对象存储

--创建有大对象字段的一张表
 create table test001
 (
       fname varchar2(50),
       content blob
 )
 
 select * from dba_users;
 
 select * from test001
 
 --(一)..准备插入大对象
 --1. 创建文件存放目录(让Oracle管理,该目录)
 create or replace directory test_dir
        as
 'e:\Pictures';
 
 --2.可以将该目录授权给其他用户访问
 grant read,write on directory test_dir to scott;
 
 --(二).准备将大对象,存放在test001表中
 declare
       tempimg blob;--定义临时变量存放数据
       tempdir bfile := bfilename('TEST_DIR','Azul.jpg');--非常重要:所有数据都是大写存放的
 begin      
       insert into test001 values ('first.jpg',empty_blob()) returning content into tempimg;
       
       --使用内置的包,给tempimg写入数据
       dbms_lob.fileopen(tempdir);--打开指定文件
       dbms_lob.loadfromfile(tempimg,tempdir,dbms_lob.getlength(tempdir));
       dbms_lob.fileclose(tempdir);--关闭文件
       
       dbms_output.put_line('恭喜你,终于成功了!!!');
       
       commit;
 end ;
 
 select * from test001
 
 select * from dba_directories
 
 
 --将Blob对象,写成磁盘文件
 declare
        l_file utl_file.file_type;--定义写入磁盘文件的类型和格式
        l_buffer raw(32767);--定义缓冲区大小
        l_amount binary_integer := 3276; --每次位移个数
        l_pos int :=1;--开始位置
        l_blob blob;--临时数据存放
        l_blob_len int;--总长度
 begin
        select content into l_blob from test001; --将数据库中的数据,存放在blob变量中
        
        --获取blob文件的长度
        l_blob_len := dbms_lob.getlength(l_blob);
        
        --准备写入磁盘文件
        l_file := utl_file.fopen('TEST_DIR','HHAHAHAHAHAHAHAHAAHA.JPG','wb');
        
        --写入数据
        while l_pos<l_blob_len loop       
              dbms_lob.read(l_blob,l_amount,l_pos,l_buffer);             
              utl_file.put_raw(l_file,l_buffer,true);             
              l_pos := l_pos + l_amount;       
        end loop;
               
        utl_file.fclose(l_file);       
        dbms_output.put_line('恭喜,恭喜。。。。文件写成功!');       
 end;
 
 
 /*
 文本大对象的写入和读取(clob)。
 */
 --写入文本文件第一种方式
 declare
       tempimg clob;--定义临时变量存放数据
       tempdir bfile := bfilename('TEST_DIR','DIV3.html');--非常重要:所有数据都是大写存放的
       amount int:=dbms_lob.getlength(tempdir);
       src_offset int:=1;
       dest_offset int:=1;
       csid int:=0;
       lc int:=0;
       warning int;
 begin      
       insert into test002 values ('第四个文本文件',empty_clob()) returning content into tempimg;
       
       --使用内置的包,给tempimg写入数据
       dbms_lob.fileopen(tempdir);--打开指定文件
       
       dbms_lob.loadclobfromfile(tempimg,tempdir,amount,dest_offset,src_offset,csid,lc,warning);
       
       dbms_lob.fileclose(tempdir);--关闭文件
       
       dbms_output.put_line('恭喜你,终于成功了!!!');
       
       commit;
 end ;
 
 --写入文本文件第二种方式(通过异常判断文件结束的)
 declare
     filecontent clob;
     input_file utl_file.file_type;
     buffer varchar2(2000);
     l_pos int := 1;
     amount int;
 begin
     insert into test002 values ('第二个文本数据',empty_clob()) returning content into filecontent;
     --打开磁盘文件
     input_file := utl_file.fopen('TEST_DIR','DIV3.html','r');
     
     loop                  
          utl_file.get_line(input_file,buffer);
          
          --获取每次读取的长度
          amount:=length(buffer);--每次写入的字符长度
          
          exit when amount<=0;
          
          dbms_lob.write(filecontent,amount,l_pos,buffer);
          
          l_pos:=l_pos+amount;
          
     end loop;
     
     utl_file.fclose(input_file);
     
     dbms_output.put_line('文件写入完毕!');
 
 exception
     when no_data_found then
     dbms_output.put_line('数据已经读取完毕了!');
     utl_file.fclose(input_file);
 end;
 
 
 select * from test002
 
 
 
 
 --读取表中的数据,到文件
 declare
     src clob;
     outfile utl_file.file_type;
     length integer;
     buffer varchar2(8000);
 begin
     select content into src from test002 where fname='第四个文本文件';
     
     length := dbms_lob.getlength(src);
     
     dbms_lob.read(src,length,1,buffer);
     
     --打开磁盘文件
     outfile := utl_file.fopen('TEST_DIR','hahahahhahahahah.html','w',8000);
     
     utl_file.put(outfile,buffer);--写入数据
     
     utl_file.fclose(outfile); --关闭指针
     
     dbms_output.put_line('文件已经写入完毕!');
 end;

posted @ 2016-08-30 21:05  Call_Me_Tiger_Fu  阅读(806)  评论(0编辑  收藏  举报