1、创建表,注意:插入图片的列要定义成BLOB类型
create table image_lob(t_id varchar2(5) not null,t_imageblob not null);
2、创建图片目录,images为目录名
create or replace directory "images" as'f:\pic\'; --创建存储图片的目录
grantcreate any directory to scott --如果需要,为scott用户授权!
3、创建存储过程,filename为图片的名字,如‘cat.jpg’
create or replace procedure img_insert(tid varchar2,filenamevarchar2)as
f_lob bfile;
b_lob blob;
begin
insert into image_lob(t_id,t_image)
values(tid,empty_blob())return t_image into b_lob; --插入空的blob
f_lob:=bfilename('images',filename);--获取指定目录下的文件
dbms_lob.fileopen(f_lob,dbms_lob.file_readonly);--以只读的方式打开文件
dbms_lob.loadfromfile(b_lob,f_lob,--传递对象
dbms_lob.getlength(f_lob));
dbms_lob.fileclose(f_lob);--关闭原始文件
commit;
end;
/
4、执行上面的存储过程,实现图片的插入
exec img_insert('1','cat.jpg');
5、查看表
select * from image_lob;
practice:
1. create table image_lob(t_id varchar2(5)not null,t_image blob not null);
2. create or replace directory"images" as 'd:\pic\';
3.
create or replace procedureimg_insert(tid varchar2,filename varchar2)as
f_lob bfile;
b_lob blob;
begin
insert into image_lob(t_id,t_image)
values(tid,empty_blob())return t_image into b_lob;
f_lob:=bfilename('images',filename);
dbms_lob.fileopen(f_lob,dbms_lob.file_readonly);
dbms_lob.loadfromfile(b_lob,f_lob,
dbms_lob.getlength(f_lob));
dbms_lob.fileclose(f_lob);
commit;
end;
4. exec img_insert('1','1.jpg');
5. select * fromimage_lob