Oracle資料庫中,使用預存程序將BLOB欄位批量導成JPG格式

來源:互聯網
上載者:User

標籤:name   replace   過程   版本   into   blob   ace   com   arch   

    環境說明:

  照片:存放在生產庫中的使用者 picmgr 中,資料庫版本為ORACLE10g;

  目的:將照片導到本地路徑 D:\image ,儲存格式為 jpg 。

 

第一步:在本地安裝oracle11g database 版本

  注意:oracle版本無所謂,10g、11g都可以,

     關鍵得是 database 版本。

  建立一個dba使用者,使用者名稱為 tonytonytony(使用者名稱隨你起) 。

  使用使用者 tonytonytony 建立 一個directory(image_dir)、

  一個表(GET_IMAGE),以及 DatabaseLink(DBLINKPIC)。

  create or replace directory image_dir as ‘D:\image‘;

  --建立用於暫時存放照片資料的表
  create table GET_IMAGE
  (
    num   NUMBER,
    xm     VARCHAR2(50),
    sfzh        VARCHAR2(18),
    filename   VARCHAR2(100),
    picture      BLOB
  );

 

第二步:資料準備及照片匯出

  create or replace procedure pro01_auto_reload_data is
  begin
    delete GET_IMAGE;  --清空表資訊
    commit;
    insert into GET_IMAGE(Xm,Sfzh,Filename,Picture)

      select a.xm,a.sfzh,a.sfzh||‘_‘||a.xm||‘.jpg‘,b.picture

      from table01 a,[email protected] b

      where a.xm=b.xm and a.sfzh=b.sfzh;
    commit;
    update GET_IMAGE set num=rownum;
    commit;
    update GET_IMAGE set picture = null where length(picture)>‘32767‘;  

    --照片大小超出範圍,則置為空白,這些照片需要手工另存。
    commit;
  end pro01_auto_reload_data;

  

  --第二個預存程序

  create or replace procedure pro02_auto_download_image(i_num in number,i_count in number) is
  v_file UTL_FILE.file_type;

  v_blob BLOB;
  v_amount BINARY_INTEGER := 32767;
  v_pos INTEGER := 1;
  v_buffer RAW(32767);

  v_blob_lenth INTEGER;
  v_filename varchar2(50);

  v_num INTEGER;
  v_count INTEGER;

  begin
    v_num := i_num;
    v_count := i_count;
    while v_num <= v_count loop
      select a.filename into v_filename from GET_IMAGE a where a.num = v_num;
      select a.picture into v_blob from GET_IMAGE a where a.num = v_num;
      v_blob_lenth := DBMS_LOB.getlength(v_blob);
      v_file := UTL_FILE.fopen(‘IMAGE_DIR‘,v_filename,‘WB‘,v_blob_lenth);
      while v_pos < v_blob_lenth loop
        dbms_lob.read(v_blob,v_amount,v_pos,v_buffer);
        UTL_FILE.put_raw(v_file,v_buffer,TRUE);
        v_pos := v_pos + v_amount;
      end loop;
      v_num := v_num + 1;
      v_pos := 1;
      UTL_FILE.fclose(v_file);
     end loop;
  end pro02_auto_download_image;

  這第二個預存程序,已經可以導照片了,但是在使用時,發現如果一次導6、7張照片,

  速度便會變的很慢,為了獲得最高的效率,我希望每次只匯出一張。

  因此我又寫了第三個預存程序。

  

第三步:實現大量匯出

  create or replace procedure pro03_auto_run_one_by_one is

  v_begin INTEGER := 1;
  v_end INTEGER := 1;
  v_count INTEGER;

  begin
    --pro01_auto_reload_data;
    select count(*) into v_count from GET_IMAGE;
    while v_begin <= v_count loop
      pro02_auto_download_image(v_begin,v_end);
      v_begin := v_begin + 1;
      v_end := v_end + 1;
      end loop;
  end pro03_auto_run_one_by_one;

  --注意:若匯出的照片大小為0KB,則需要手工做另存的操作了。

 

  使用的方法就是,先執行第一個預存程序,將照片資訊轉存到本機資料庫的表GET_IMAGE中,

  然後再執行第三個預存程序。

 

Oracle資料庫中,使用預存程序將BLOB欄位批量導成JPG格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.