Hey,有没有想过在Oracle数据库里直接存储图片?别再只是保存文件路径了,直接存储二进制数据,操作起来更高效!今天就来聊聊如何在Oracle中直接存储图片数据。
首先,创建一个表,记得将存储图片的列定义为BLOB类型:
create table image_lob(t_id varchar2(5) not null,t_image blob not null);
然后,创建一个图片目录,并授权:
create or replace directory IMAGE as ‘/home/oracle/image’;
grant read,write on directory IMAGE to chester;
接下来,上传图片到该目录。
现在,创建一个存储过程来插入图片:
create or replace procedure img_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('IMAGE', 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;
执行这个存储过程,就可以实现图片的插入:
begin
img_insert(‘1’,‘abc.png’);
end;
最后,查看表中的数据:
select * from image_lob;
怎么样,是不是很简单?直接在数据库里存储图片,方便又高效。更多Oracle数据库的技巧,尽在「极星编程网」(www.jxgpc.com),我是苏承栈,我们下期再见!
