向Oracle的blob字段导入文件

在数据库主机上创建测试目录及文件

$mkdir /test

$cd /test

$echo "Test Subject" >> subject.html

$echo "test ok !" >> mail.html

定义文件路径(都是数据库主机上的),并授权

$sqlplus user/passwd@instance

SQL>create or replace directory send_file_dir as '/test';

SQL>grant read on directory send_file_dir to test1;

如下是向发邮件的表中插入内容(表中有两个blob字段subject,message)

$sqlplus user/passwd@instance

SQL>declare

destlocblob;

destmsg blob;

srcfilebfile:=BFILENAME('SEND_FILE_DIR','subject.html');

msgfilebfile:=BFILENAME('SEND_FILE_DIR','mail.html');

BEGIN

insert into email_send values (seq_email_id.nextval,null,'[email protected]',null,null,empty_blob(),empty_blob(),sysdate,null,'waiting',0,'HTML',null)

returning subject,message into destloc,destmsg; --这里如果有多个blob字段再增加。

dbms_lob.fileopen(srcfile);

dbms_lob.loadfromfile(destloc,srcfile,dbms_lob.getlength(srcfile));

dbms_lob.fileclose(srcfile);

dbms_lob.fileopen(msgfile);

dbms_lob.loadfromfile(destmsg,msgfile,dbms_lob.getlength(msgfile));

dbms_lob.fileclose(msgfile);

commit;

END;

这样就向表email_send插入了两个blob字段。

subject字段内容为subject.html

message字段内容为mail.html

报错信息

ERROR at line 1:

ORA-22288: file or LOB operation FILEOPEN failed

No such file or directory

ORA-06512: at "SYS.DBMS_LOB", line 805

ORA-06512: at line 9

解决方法

这些文件都是在数据库主机上的,如果主机上没有这些文件都会报错。在主机上创建目录及文件就ok了,我暂时还没找到从远程上读取文件的方法,如果哪位兄弟知道的话,告诉下,谢谢!!

猜你喜欢

转载自www.linuxidc.com/Linux/2017-02/140579.htm