• Oracle的Lob大对象操作


      1 --创建有大对象字段的一张表
      2 create table test001
      3 (
      4       fname varchar2(50),
      5       content blob
      6 )
      7 
      8 select * from dba_users;
      9 
     10 select * from test001
     11 
     12 --(一)..准备插入大对象
     13 --1. 创建文件存放目录(让Oracle管理,该目录)
     14 create or replace directory test_dir
     15        as
     16 'e:\Pictures';
     17 
     18 --2.可以将该目录授权给其他用户访问
     19 grant read,write on directory test_dir to scott;
     20 
     21 --(二).准备将大对象,存放在test001表中
     22 declare
     23       tempimg blob;--定义临时变量存放数据
     24       tempdir bfile := bfilename('TEST_DIR','Azul.jpg');--非常重要:所有数据都是大写存放的
     25 begin      
     26       insert into test001 values ('first.jpg',empty_blob()) returning content into tempimg;
     27       
     28       --使用内置的包,给tempimg写入数据
     29       dbms_lob.fileopen(tempdir);--打开指定文件
     30       dbms_lob.loadfromfile(tempimg,tempdir,dbms_lob.getlength(tempdir));
     31       dbms_lob.fileclose(tempdir);--关闭文件
     32       
     33       dbms_output.put_line('恭喜你,终于成功了!!!');
     34       
     35       commit;
     36 end ;
     37 
     38 select * from test001
     39 
     40 select * from dba_directories
     41 
     42 
     43 --将Blob对象,写成磁盘文件
     44 declare
     45        l_file utl_file.file_type;--定义写入磁盘文件的类型和格式
     46        l_buffer raw(32767);--定义缓冲区大小
     47        l_amount binary_integer := 3276; --每次位移个数
     48        l_pos int :=1;--开始位置
     49        l_blob blob;--临时数据存放
     50        l_blob_len int;--总长度
     51 begin
     52        select content into l_blob from test001; --将数据库中的数据,存放在blob变量中
     53        
     54        --获取blob文件的长度
     55        l_blob_len := dbms_lob.getlength(l_blob);
     56        
     57        --准备写入磁盘文件
     58        l_file := utl_file.fopen('TEST_DIR','HHAHAHAHAHAHAHAHAAHA.JPG','wb');
     59        
     60        --写入数据
     61        while l_pos<l_blob_len loop       
     62              dbms_lob.read(l_blob,l_amount,l_pos,l_buffer);             
     63              utl_file.put_raw(l_file,l_buffer,true);             
     64              l_pos := l_pos + l_amount;       
     65        end loop;
     66               
     67        utl_file.fclose(l_file);       
     68        dbms_output.put_line('恭喜,恭喜。。。。文件写成功!');       
     69 end;
     70 
     71 
     72 /*
     73 文本大对象的写入和读取(clob)。
     74 */
     75 --写入文本文件第一种方式
     76 declare
     77       tempimg clob;--定义临时变量存放数据
     78       tempdir bfile := bfilename('TEST_DIR','DIV3.html');--非常重要:所有数据都是大写存放的
     79       amount int:=dbms_lob.getlength(tempdir);
     80       src_offset int:=1;
     81       dest_offset int:=1;
     82       csid int:=0;
     83       lc int:=0;
     84       warning int;
     85 begin      
     86       insert into test002 values ('第四个文本文件',empty_clob()) returning content into tempimg;
     87       
     88       --使用内置的包,给tempimg写入数据
     89       dbms_lob.fileopen(tempdir);--打开指定文件
     90       
     91       dbms_lob.loadclobfromfile(tempimg,tempdir,amount,dest_offset,src_offset,csid,lc,warning);
     92       
     93       dbms_lob.fileclose(tempdir);--关闭文件
     94       
     95       dbms_output.put_line('恭喜你,终于成功了!!!');
     96       
     97       commit;
     98 end ;
     99 
    100 --写入文本文件第二种方式(通过异常判断文件结束的)
    101 declare
    102     filecontent clob;
    103     input_file utl_file.file_type;
    104     buffer varchar2(2000);
    105     l_pos int := 1;
    106     amount int;
    107 begin
    108     insert into test002 values ('第二个文本数据',empty_clob()) returning content into filecontent;
    109     --打开磁盘文件
    110     input_file := utl_file.fopen('TEST_DIR','DIV3.html','r');
    111     
    112     loop                  
    113          utl_file.get_line(input_file,buffer);
    114          
    115          --获取每次读取的长度
    116          amount:=length(buffer);--每次写入的字符长度
    117          
    118          exit when amount<=0;
    119          
    120          dbms_lob.write(filecontent,amount,l_pos,buffer);
    121          
    122          l_pos:=l_pos+amount;
    123          
    124     end loop;
    125     
    126     utl_file.fclose(input_file);
    127     
    128     dbms_output.put_line('文件写入完毕!');
    129 
    130 exception 
    131     when no_data_found then
    132     dbms_output.put_line('数据已经读取完毕了!');
    133     utl_file.fclose(input_file);
    134 end;
    135 
    136 
    137 select * from test002
    138 
    139 
    140 
    141 
    142 --读取表中的数据,到文件
    143 declare
    144     src clob;
    145     outfile utl_file.file_type;
    146     length integer;
    147     buffer varchar2(8000);
    148 begin
    149     select content into src from test002 where fname='第四个文本文件';
    150     
    151     length := dbms_lob.getlength(src);
    152     
    153     dbms_lob.read(src,length,1,buffer);
    154     
    155     --打开磁盘文件
    156     outfile := utl_file.fopen('TEST_DIR','hahahahhahahahah.html','w',8000);
    157     
    158     utl_file.put(outfile,buffer);--写入数据
    159     
    160     utl_file.fclose(outfile); --关闭指针
    161     
    162     dbms_output.put_line('文件已经写入完毕!');
    163 end;
  • 相关阅读:
    python征程1.4(初识python)
    python征程1.3(初识python)
    python征程1.2(初识python)
    python征程1.1(初识python)
    什么是servlet
    什么是JavaBean
    基于Ajax的前后端分离
    项目开发流程
    项目框架和项目架构
    Java中使用 foreach 操作数组
  • 原文地址:https://www.cnblogs.com/huzi007/p/2874507.html
Copyright © 2020-2023  润新知