访问blob

– 1,建立blob的表
create table lob_ex2 (
id number(4) primary key,
name varchar2(20),
photo blob
);

– 2,初始化blob
insert into lob_ex2 values(‘1’,’王瑶’,empty_blob());
insert into lob_ex2 values(‘2’,’梁涛’,empty_blob());

—3 ,将二进制写入blob列
create or replace directory M_F as ‘F:\金智课堂小宝\2x_web’;
select * from dba_directories;

declare
lobloc blob;
fileloc bfile;
v_acount int;
dest_offset int := 1;
src_offset int := 1;
begin
select photo into lobloc from lob_ex2 where id = 1 for update;
fileloc:= bfilename(‘M_F’,’wang.png’);
dbms_lob.fileopen(fileloc,0);
v_acount:= dbms_lob.getlength(fileloc);
dbms_lob.loadblobfromfile(lobloc,fileloc,v_acount,dest_offset,src_offset);
dbms_lob.fileclose(fileloc);
end;

select * from lob_ex2;

–4,读blob数据类型
declare
lobloc blob;
v_acount int ;
buffer raw(2000);

begin
select photo into lobloc from lob_ex2 where id = 1;
v_acount:= dbms_lob.getlength(lobloc);
dbms_lob.read(lobloc,v_acount,1,buffer);
end;

–5,将blob 数据写入二进制文档
/*
不仅需要dbms_lob包
还需要utl_file建立二进制文件并写入内容;
*/
create or replace directory M_PUT as ‘F:\’;

declare
lobloc blob;
v_acount int ;
dest_offset int := 1;
buffer raw(2000);
handle utl_file.file_type;
begin
select photo into lobloc from lob_ex2 where id = 1;
v_acount:= dbms_lob.getlength(lobloc);
dbms_lob.read(lobloc,v_acount,1,buffer);
handle:= utl_file.fopen(‘M_PUT’,’wangyao.png’,’W’,2000);
utl_file.put_raw(handle,buffer);
utl_file.fclose(handle);
end;

你可能感兴趣的:(总结)