以下的文章主要介紹的是Oracle數據庫如何正確用PL/SQL從相關數據庫中來准確的讀取BLOB字段的實際操作,其中包括如何正確的確認相關對象存在,以及如何創建存儲Directory等相關內容介紹。
1.確認對象存在
- SQL> col fdesc for a30
- SQL> select fid,fname,fdesc from eygle_blob;
- FID FNAME FDESC
1 ShaoLin.jpg 少林寺-易經經
2 DaoYing.jpg 倒映
2.創建存儲Directory
- SQL> connect / as sysdba
- Connected.
- SQL> create or replace directory BLOBDIR as 'D:\oradata\Pic';
- Directory created.
- SQL>
- SQL> grant read,write on directory BLOBDIR to eygle;
- Grant succeeded.
- SQL>
3.Oracle數據庫用PL/SQL從數據庫中讀取BLOB字段的過程中我們需要創建存儲過程
- SQL> connect eygle/eygle
- Connected.
- SQL>
- SQL> CREATE OR REPLACE PROCEDURE eygle_dump_blob (piname varchar2,poname varchar2) IS
- l_file UTL_FILE.FILE_TYPE;
- l_buffer RAW(32767);
- l_amount BINARY_INTEGER := 32767;
- l_pos INTEGER := 1;
- l_blob BLOB;
- l_blob_len INTEGER;
- BEGIN
- SELECT FPIC
- INTO l_blob
- FROM eygle_blob
- WHERE FNAME = piname;
- l_blob_len := DBMS_LOB.GETLENGTH(l_blob);
- l_file := UTL_FILE.FOPEN('BLOBDIR',poname,'wb', 32767);
- 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);
- EXCEPTION
- WHEN OTHERS THEN
- IF UTL_FILE.IS_OPEN(l_file) THEN
- UTL_FILE.FCLOSE(l_file);
- END IF;
- RAISE;
- 31 END;
- 32 /
- Procedure created.
4.取出字段數據
- SQL> host ls -l d:\oradata\Pic
- total 7618
- -rwxrwxrwa 1 gqgai None 2131553 Apr 19 10:12 DaoYing.jpg
- -rwxrwxrwa 1 gqgai None 1768198 Apr 19 10:12 ShaoLin.jpg
- SQL> exec eygle_dump_blob('ShaoLin.jpg','01.jpg')
- PL/SQL procedure successfully completed.
- SQL> host ls -l d:\oradata\Pic
- total 11072
- -rwxrwxrwa 1 Administrators SYSTEM 1768198 Apr 26 07:16 01.jpg
- -rwxrwxrwa 1 gqgai None 2131553 Apr 19 10:12 DaoYing.jpg
- -rwxrwxrwa 1 gqgai None 1768198 Apr 19 10:12 ShaoLin.jpg
- SQL>
- SQL> exec eygle_dump_blob('DaoYing.jpg','02.jpg')
- PL/SQL procedure successfully completed.
- SQL> host ls -l d:\oradata\Pic
- total 15236
- -rwxrwxrwa 1 Administrators SYSTEM 1768198 Apr 26 07:16 01.jpg
- -rwxrwxrwa 1 Administrators SYSTEM 2131553 Apr 26 07:19 02.jpg
- -rwxrwxrwa 1 gqgai None 2131553 Apr 19 10:12 DaoYing.jpg
- -rwxrwxrwa 1 gqgai None 1768198 Apr 19 10:12 ShaoLin.jpg
上述的相關內容就是對Oracle數據庫用PL/SQL從數據庫中讀取BLOB字段的描述,希望會給你帶來一些幫助在此方面。