BLOB文件的导出
导入咱就不说了,因为暂时还不会,但是导出咱会拉,从网上找了资料,在此感谢ITEYE的lzj520的共享
out_put_photo是启动程序,我在PL/SQL Developer里面尝试用的SQL命令板上面打上命令
begin
out_put_photo;
end;
按齿轮按钮执行即可。
--out_put_photo
create or replace procedure out_put_photo is
IDENTITYID varchar2(20);
cursor cur is
select IDENTITY_ID from TB_SUCCESS;
cur_result cur%rowtype;
begin
if cur%isopen = false then --we don't need to check that, cause we are the script creator,
open cur; -- we know when we need to open the cursor.
end if;
loop
fetch cur into cur_result;
exit when cur%notfound;
IDENTITYID:=cur_result.IDENTITY_ID;
photo_dump(identityid => IDENTITYID,
filename => IDENTITYID||'.jpg'); --we can also use the statement [photo_dump(IDENTITYID,IDENTITYID||'.jpg')] to run the procedure.
end loop;
close cur;
end out_put_photo;
--photo_dump
create or replace procedure photo_dump(IDENTITYID in varchar2,filename in varchar2) is
l_file UTL_FILE.FILE_TYPE;
l_buffer RAW(32767);
l_amount BINARY_INTEGER := 32767;
l_pos INTEGER := 1;
l_blob BLOB;
l_blob_len INTEGER;
begin
SELECT PHOTO
INTO l_blob
FROM TB_SUCCESS
WHERE IDENTITY_ID = IDENTITYID;
l_blob_len := DBMS_LOB.GETLENGTH(l_blob);
l_file := UTL_FILE.FOPEN('BATCH_WRITE',filename,'wb', l_blob_len); --because what we'll write is a binary data, so we must use the parameter [wb], not [w]
WHILE l_pos < l_blob_len LOOP --just like what we've done in java, we need to write this binary data with a while loop,
DBMS_LOB.READ (l_blob, l_amount, l_pos, l_buffer); --and to deal by the max raw unit(32767).
UTL_FILE.PUT_RAW(l_file, l_buffer, TRUE);
l_pos := l_pos + l_amount;
END LOOP;
UTL_FILE.FCLOSE(l_file); --remember to close the I/O process id.
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
IF UTL_FILE.IS_OPEN(l_file) THEN --even an error occured.
UTL_FILE.FCLOSE(l_file);
END IF;
RAISE;
end photo_dump;
建表语句是
create table TB_SUCCESS(
IDENTITY_ID varchar2(20),
PHOTO blob
);
操作步骤:
一:
表建成后,我是直接在PL/SQL Developer里面Edit Data,BLOB项目是通过
点击项目右边的扩展按钮([…]),选择Image的Tab块之后,打开选中某*.jpg图片
打开后点击OK保存即可。
二:
利用
CREATE DIRECTORY BATCH_WRITE AS 'D:\batch';
GRANT WRITE ON DIRECTORY BATCH_WRITE TO MY_USER;
给当前用户赋予写权限。
检证结果用语句 SELECT * FROM ALL_DIRECTORIES;
三:
执行
out_put_photo。
四:
查看DB服务器D:\batch目录下生成了文件(01.jpg,02.jpg)。