How does OraclePL read BLOB fields from the database?

Source: Internet
Author: User

The following articles mainly describe how OraclePL and SQL read BLOB fields from the database. If you are interested in the actual operations, the following articles will provide you with relevant knowledge. I hope you will gain some benefits.

1. confirm that the object exists

 
 
  1. SQL> col fdesc for a30  
  2. SQL> select fid,fname,fdesc from eygle_blob; 

FID FNAME FDESC

1 ShaoLin.jpg Shaolin Temple-Yijing

2 ing DaoYing.jpg

2. Create a storage Directory

 
 
  1. SQL> connect / as sysdba  
  2. Connected.  
  3. SQL> create or replace directory BLOBDIR as 'D:\oradata\Pic';  
  4. Directory created.  
  5. SQL> 
  6. SQL> grant read,write on directory BLOBDIR to eygle;  
  7. Grant succeeded.  
  8. SQL> 
  9.  

3. Create a stored procedure

 
 
  1. SQL> connect eygle/eygle  
  2. Connected.  
  3. SQL> 
  4. SQL> CREATE OR REPLACE PROCEDURE eygle_dump_blob (piname varchar2,poname varchar2) IS  
  5. l_file UTL_FILE.FILE_TYPE;  
  6. l_buffer RAW(32767);  
  7. l_amount BINARY_INTEGER := 32767;  
  8. l_pos INTEGER := 1;  
  9. l_blob BLOB;  
  10. l_blob_len INTEGER;  
  11. BEGIN  
  12. SELECT FPIC  
  13. INTO l_blob  
  14. FROM eygle_blob  
  15. WHERE FNAME = piname;  
  16. l_blob_len := DBMS_LOB.GETLENGTH(l_blob);  
  17. l_file := UTL_FILE.FOPEN('BLOBDIR',poname,'wb', 32767);  
  18. WHILE l_pos < l_blob_len LOOP  
  19. DBMS_LOB.READ (l_blob, l_amount, l_pos, l_buffer);  
  20. UTL_FILE.PUT_RAW(l_file, l_buffer, TRUE);  
  21. l_pos := l_pos + l_amount;  
  22. END LOOP;  
  23. UTL_FILE.FCLOSE(l_file);  
  24. EXCEPTION  
  25. WHEN OTHERS THEN  
  26. IF UTL_FILE.IS_OPEN(l_file) THEN  
  27. UTL_FILE.FCLOSE(l_file);  
  28. END IF;  
  29. RAISE;  
  30. END;  
  31. /  
  32. Procedure created.  

4. Retrieve field data

 
 
  1. SQL> host ls -l d:\oradata\Pic  
  2. total 7618  
  3. -rwxrwxrwa 1 gqgai None 2131553 Apr 19 10:12 DaoYing.jpg  
  4. -rwxrwxrwa 1 gqgai None 1768198 Apr 19 10:12 ShaoLin.jpg  
  5. SQL> exec eygle_dump_blob('ShaoLin.jpg','01.jpg')  
  6. PL/SQL procedure successfully completed.  
  7. SQL> host ls -l d:\oradata\Pic  
  8. total 11072  
  9. -rwxrwxrwa 1 Administrators SYSTEM 1768198 Apr 26 07:16 01.jpg  
  10. -rwxrwxrwa 1 gqgai None 2131553 Apr 19 10:12 DaoYing.jpg  
  11. -rwxrwxrwa 1 gqgai None 1768198 Apr 19 10:12 ShaoLin.jpg  
  12. SQL> 
  13. SQL> exec eygle_dump_blob('DaoYing.jpg','02.jpg')  
  14. PL/SQL procedure successfully completed.  
  15. SQL> host ls -l d:\oradata\Pic  
  16. total 15236  
  17. -rwxrwxrwa 1 Administrators SYSTEM 1768198 Apr 26 07:16 01.jpg  
  18. -rwxrwxrwa 1 Administrators SYSTEM 2131553 Apr 26 07:19 02.jpg  
  19. -rwxrwxrwa 1 gqgai None 2131553 Apr 19 10:12 DaoYing.jpg  
  20. -rwxrwxrwa 1 gqgai None 1768198 Apr 19 10:12 ShaoLin.jpg  

The above content is an introduction to reading BLOB fields from the database by OraclePL and SQL. I hope you will gain some benefits.

Article by: http://www.programbbs.com/doc/4255.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.