• Oracle操作大对象BLOB示例


    --创建一张测试表

    create table test2

    (  

        tid int primary key,   

      tname varchar2(20),   

      tcontent blob --数据类型为blob

    )

    --创建普通序列号

    create sequence sequ_test2

    --写入图片

    declare   

    tempimg blob;--定义临时变量存放数据   

    tempdir bfile := bfilename('TEST_DIR','Koala.jpg');--非常重要:所有数据都是大写存放的

    begin          

      insert into test2 values (sequ_test2.nextval,'Koala.jpg',empty_blob())

      returning tcontent 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 ;

    --写入音乐

    declare      

      tempimg blob;--定义临时变量存放数据      

         tempdir bfile := bfilename('TEST_DIR','Kalimba.mp3');--非常重要:所有数据都是大写存放的

    begin  

              insert into test2 values (sequ_test2.nextval,'Kalimba.mp3',empty_blob()) returning tcontent 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 ;

    --将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 tcontent into l_blob from test2 where tid=2;

      --将数据库中的数据,存放在blob变量中              

      --获取blob文件的长度       

      l_blob_len := dbms_lob.getlength(l_blob);              

       --准备写入磁盘文件       

       l_file := utl_file.fopen('TEST_DIR','xxx.mp3','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;

  • 相关阅读:
    Codeforces Round #610 (Div. 2)C(贪心,思维)
    Educational Codeforces Round 80 (Rated for Div. 2)C(DP)
    BZOJ2190 仪仗队
    BZOJ3613 南园满地堆轻絮
    BZOJ1084 最大子矩阵
    BZOJ1036 树的统计Count
    BZOJ1452 Count
    BZOJ2242 计算器
    BZOJ2705 Longge的问题
    BZOJ1509 逃学的小孩
  • 原文地址:https://www.cnblogs.com/wshan/p/2873654.html
Copyright © 2020-2023  润新知