PL/SQL File to Blob
Hello,
Converting physical files into binary data type is an important requirement. In this tutorial, I am going to explain converting files into blob data type.
Basically, Oracle has two large object data types.
- BLOB (Binary Large Object): Used for keeping images, binary documents such as word, excel, pdf..
- CLOB (Character Large Object): Used for keeping huge strings where varchar2 is not enough.
BFILE is also a data type which points or locates physical file.
Follow these steps in order to convert file to blob data type.
- Create database directory which is set as the physical file’s path.
- Define BLOB and BFILE variables.
- Insert empty blob into table.
- Return blob data type into blob variable.
- Open blob and bfile.
- Load bfile into blob by using dbms_lob package.
- Close all lob objects.
- Commit.
Create table with following script:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
create table xx_oracle_text_ornek ( sira_id number, dokuman blob, kayit_tarihi date ); alter table hr.xx_oracle_text_ornek add constraint xx_oracle_text_ornek_pk primary key (sira_id); |
Create database directory object. This object is the equivalent of the physical file in machine. Please pay attention that the path you define is physically exist.
1 2 3 |
create or replace directory xx_win_anil_dir as 'c:\blog'; |
Declare BLOB and BFILE variables:
1 2 3 4 5 |
declare l_blob_dosya blob; l_dosya bfile := bfilename('XX_WIN_ANIL_DIR', 'a.pdf'); |
Insert empty blob into table and table column into blob variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
begin insert into xx_oracle_text_ornek ( sira_id, dokuman, kayit_tarihi ) values ( 1, empty_blob(), sysdate ) returning dokuman into l_blob_dosya; /*select dokuman into l_blob_dosya from xx_oracle_text_ornek where sira_id = 1;*/ |
Open lobs in read and read-write modes.
1 2 3 4 |
dbms_lob.open(l_dosya, dbms_lob.lob_readonly); dbms_lob.open(l_blob_dosya, dbms_lob.lob_readwrite); |
Load binary data into blob variable from bfile by using dbms_lob.loadfromfile.
1 2 3 4 5 |
dbms_lob.loadfromfile(dest_lob => l_blob_dosya, src_lob => l_dosya, amount => dbms_lob.getlength(l_dosya)); |
Close the lobs and commit the transaction.
1 2 3 4 5 6 |
dbms_lob.close(l_dosya); dbms_lob.close(l_blob_dosya); commit; |
All script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
declare l_blob_dosya blob; l_dosya bfile := bfilename('XX_WIN_ANIL_DIR', 'a.pdf'); begin insert into xx_oracle_text_ornek ( sira_id, dokuman, kayit_tarihi ) values ( 1, empty_blob(), sysdate ) returning dokuman into l_blob_dosya; /*select dokuman into l_blob_dosya from xx_oracle_text_ornek where sira_id = 1;*/ dbms_lob.open(l_dosya, dbms_lob.lob_readonly); dbms_lob.open(l_blob_dosya, dbms_lob.lob_readwrite); dbms_lob.loadfromfile(dest_lob => l_blob_dosya, src_lob => l_dosya, amount => dbms_lob.getlength(l_dosya)); dbms_lob.close(l_dosya); dbms_lob.close(l_blob_dosya); commit; end; |
thanks, it is very helpful.
Voici le message d’erreur que j’ai une fois exécute le programme anonyme:
Rapport d’erreur –
ORA-22285: répertoire ou fichier inexistant pour l’opération FILEOPEN
ORA-06512: à “SYS.DBMS_LOB”, ligne 1014
ORA-06512: à ligne 20
22285. 00000 – “non-existent directory or file for %s operation”
*Cause: Attempted to access a directory that does not exist, or attempted
to access a file in a directory that does not exist.
*Action: Ensure that a system object corresponding to the specified
directory exists in the database dictionary, or
make sure the name is correct