《在Oracle数据库的表中插入图片的方法.docx》由会员分享,可在线阅读,更多相关《在Oracle数据库的表中插入图片的方法.docx(2页珍藏版)》请在三一办公上搜索。
1、在Oracle数据库的表中插入图片的方法在Oracle数据库的表中插入图片的方法 1、创建表,注意:插入图片的列要定义成BLOB类型 create table image_lob(t_id varchar2(5) not null,t_image blob not null); 2、创建图片目录,images为目录名 create or replace directory images as f:pic; -创建存储图片的目录 grant create any directory to scott -如果需要,为scott用户授权! 3、创建存储过程,filename为图片的名字,如cat.j
2、pg 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; -插入空的blob f_lob:=bfilename(images,filename);- 获取指定目录下的文件 dbms_lob.fileopen(f_lob,dbms_lob.file_readonly);- 以
3、只读的方式打开文件 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. crea
4、te or replace directory images as d:pic; 3. 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(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,bg.jpg); 5. select * from image_lob