Thursday, November 18, 2010

How to plugin sql into Eclipse?

Help -> Install New Software -> Work With :
http://eclipsesql.sourceforge.net/

Select SQL Exporter, Click Next, Click Finish.

File -> New -> other , SQL Development -> SQL File,
Enter file name, create database (any) connection throuh wizard and
click finish.

Saturday, November 13, 2010

How to write blob into OS file ?

1. create a database directory :
create or replace database directory image_test_dir
as 'd:\db_dir\'

2. use dbms_lob and utl_file package to read data chunk wise.



declare
v_offset number := 1;
v_buffer_size number := 1000;
v_file_handle UTL_FILE.FILE_TYPE;
v_src_lob blob;
v_len integer;
v_buffer raw(1000);

begin
/*select image_front into v_src_lob
from inward_image where rownum=1;*/

select img into v_src_lob
from image_upload where rownum=1;

v_len := DBMS_LOB.GETLENGTH(v_src_lob);

v_file_handle := UTL_FILE.FOPEN('IMAGE_TEST_DIR', 'f.pdf', 'wb');

while v_offset < v_Len
loop

DBMS_LOB.READ(v_src_lob, v_buffer_size, v_offset, v_buffer );

UTL_FILE.PUT_RAW(v_file_handle, v_buffer);

--DBMS_OUTPUT.PUT_LINE( v_offset || ' ' || v_buffer_size );

v_offset := v_offset + v_buffer_size;


end loop;


--DBMS_OUTPUT.PUT_LINE();

UTL_FILE.FFLUSH(v_file_handle);

UTL_FILE.FCLOSE(v_file_handle);


end;

how to load os file into blob field ?

If you want to to load a file (i.e. text file, image file, pdf file etc.) into
a blob field, then follow the following procedure.

1. create a database directory :
create or replace database directory image_test_dir
as 'd:\db_dir\'

2. Use dbms_Lob package to load os file into blob;

declare

v_file_loc bfile;

v_Image_size integer;

v_img blob;

begin

--v_file_loc := bfilename('IMAGE_TEST_DIR', 'testRpt.pdf');
v_file_loc := bfilename('IMAGE_TEST_DIR', 'front.tif');

v_image_size := DBMS_LOB.GETLENGTH(v_file_Loc);

dbms_output.put_line(v_image_size);

insert into image_upload(id, img)
values (1, empty_blob()) returning img into v_img;

dbms_Lob.open(v_file_Loc, DBMS_LOB.FILE_READONLY );

DBMS_LOB.LOADFROMFILE( v_img,
v_file_loc,
v_image_size );

DBMS_LOB.CLOSE(v_file_Loc);

commit;


exception
when others then dbms_output.put_line('Exception : ' || sqlerrm);

end;