BLOB方面的學習筆記

jixuewen發表於2007-12-11

自己的學習筆記,可以將一個片什麼 的搞到資料庫中,也可以將一個電影從資料庫中匯出來。

當然對於word之類的文件也是可以的。喜歡玩的朋友直接將裡面 貼進去就可以用,如果想改為過程,只要將declare部分改改就可以了。改成fuction也是可以的。另外將select into xx where部分改為變數就可以成為一個過程了。

下面是學習筆記:

使用之前要有directory授權:
grant create any directory to hr;
create or replace directory D as 'd:work';
建立包含bfile列的表:
create table lob_example3(
id number(6) primary key,
name varchar2(10),
resume bfile);

insert into lob_example3 values(1,'luojia',bfilename('D','a.sql'));
寫完東西要從裡面讀:
declare
buffer raw(2000);
amount int;
offset int;
lob_loc bfile;
begin
select resume into lob_loc from lob_example3
where id=&id;
dbms_lob.fileopen(lob_loc,0);
amount:=dbms_lob.getlength(lob_loc);
offset:=1;
dbms_lob.read(lob_loc,amount,offset,buffer);
dbms_output.put_line(buffer);
dbms_lob.fileclose(lob_loc);
end;
/


declare
dest_lob clob;
src_lob clob;
begin
src_lob:='中國';
dest_lob:='&content';
if dbms_lob.compare(src_lob,dest_lob)=0 then
dbms_output.put_line('same');
else
dbms_output.put_line('different');
end if;
end;

declare
file1 bfile;
length int;
begin
file1:=bfilename('D','a.sql');
length:=dbms_lob.getlength(file1);
dbms_output.put_line('file length:'||length);
end;
/


declare
src_lob clob:='中國,中國,
偉大的中國';
amount int;
v1 varchar2(200);
offset int;
begin
amount:=10;
offset:=3;
v1:=dbms_lob.substr('中國1234567890',amount,offset);
dbms_output.put_line(v1);
end;
/
使用clob:
create table lob_example1(
id number(6) primary key,
name varchar2(10),
resume clob);

insert into lob_example1 values(1,'luojia',empty_clob());

往lob中寫東西
declare
lob_loc clob;
text varchar2(200);
amount int;
offset int;
begin
select resume into lob_loc from lob_example1
where id=&id for update;
offset:=dbms_lob.getlength(lob_loc)+1;
text:='&resume';
amount:=length(text);
dbms_lob.write(lob_loc,amount,offset,text);
commit;
end;
/
寫完東西要從裡面讀:
declare
lob_loc clob;
buffer varchar2(200);
amount int;
offset int;
begin
select resume into lob_loc from lob_example1
where id=&id;
offset:=14;
amount:=dbms_lob.getlength(lob_loc);
dbms_lob.read(lob_loc,amount,offset,buffer);
dbms_output.put_line(buffer);
end;
/
使用blob,這裡是將一個檔案寫入到blob中。
declare
lobloc blob;
fileloc bfile;
amount int;
src_offset int:=1;
dest_offset int:=1;
begin
select resume into lobloc from lob_example
where id=&id for update;
fileloc:=bfilename('D','&filename');
dbms_lob.fileopen(fileloc,0);
amount:=dbms_lob.getlength(fileloc);
dbms_lob.loadblobfromfile(lobloc,fileloc,amount,dest_offset,
src_offset);
dbms_lob.fileclose(fileloc);
end;
/
完成寫入後,可以將blob中的檔案寫出到二進位制檔案中,但是這個需要使用utl_file包:
declare
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 resume into l_blob from lob_example
where id=1;
l_blob_len := DBMS_LOB.GETLENGTH(l_blob);
l_file := UTL_FILE.FOPEN('D','a.mp3','wb', 32767);
WHILE l_pos < l_blob_len LOOP
DBMS_LOB.READ (l_blob, l_amount, l_pos, l_buffer);
UTL_FILE.PUT_RAW(l_file, l_buffer, TRUE);
l_pos := l_pos + l_amount;
END LOOP;
UTL_FILE.FCLOSE(l_file);
end;
/

使用UTL_FILE轉儲資料為逗號分隔符檔案
Tom寫過這樣一個函式用於把資料轉儲為逗號分隔符檔案,看到很多人問類似的問題,轉載這裡供參考。
注意,UTL_FILE使用的Directory,需要你預先建立,具體可以參考Using Create directory & UTL_FILE in Oracle
create or replace function dump_csv( p_query in varchar2,
p_separator in varchar2
default ',',
p_dir in varchar2 ,
p_filename in varchar2 )
return number
AUTHID CURRENT_USER
is
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(2000);
l_status integer;
l_colCnt number default 0;
l_separator varchar2(10) default '';
l_cnt number default 0;
begin
l_output := utl_file.fopen( p_dir, p_filename, 'w' );

dbms_sql.parse( l_theCursor, p_query, dbms_sql.native );

for i in 1 .. 255 loop
begin
dbms_sql.define_column( l_theCursor, i,
l_columnValue, 2000 );
l_colCnt := i;
exception
when others then
if ( sqlcode = -1007 ) then exit;
else
raise;
end if;
end;
end loop;

dbms_sql.define_column( l_theCursor, 1, l_columnValue,
2000 );

l_status := dbms_sql.execute(l_theCursor);

loop
exit when ( dbms_sql.fetch_rows(l_theCursor) <= 0 );
l_separator := '';
for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i,
l_columnValue );
utl_file.put( l_output, l_separator ||
l_columnValue );
l_separator := p_separator;
end loop;
utl_file.new_line( l_output );
l_cnt := l_cnt+1;
end loop;
dbms_sql.close_cursor(l_theCursor);

utl_file.fclose( l_output );
return l_cnt;
end dump_csv;
/


以下是使用樣例:

[oracle@jumper tmp]$ sqlplus "/ as sysdba"

SQL*Plus: Release 9.2.0.4.0 - Production on Fri May 13 15:04:41 2005

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.


Connected to:
Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning option
JServer Release 9.2.0.4.0 - Production

SQL> select dump_csv('select * from scott.emp',',','/tmp','emp.csv') from dual;

DUMP_CSV('SELECT*FROMSCOTT.EMP',',','/TMP','EMP.CSV')
-----------------------------------------------------
14

SQL> ! more /tmp/emp.csv
7369,SMITH,CLERK,7902,17-DEC-80,800,,20
7499,ALLEN,SALESMAN,7698,20-FEB-81,1600,300,30
7521,WARD,SALESMAN,7698,22-FEB-81,1250,500,30
7566,JONES,MANAGER,7839,02-APR-81,2975,,20
7654,MARTIN,SALESMAN,7698,28-SEP-81,1250,1400,30
7698,BLAKE,MANAGER,7839,01-MAY-81,2850,,30
7782,CLARK,MANAGER,7839,09-JUN-81,2450,,10
7788,SCOTT,ANALYST,7566,19-APR-87,3000,,20
7839,KING,PRESIDENT,,17-NOV-81,5000,,10
7844,TURNER,SALESMAN,7698,08-SEP-81,1500,0,30
7876,ADAMS,CLERK,7788,23-MAY-87,1100,,20
7900,JAMES,CLERK,7698,03-DEC-81,950,,30
7902,FORD,ANALYST,7566,03-DEC-81,3000,,20
7934,MILLER,CLERK,7782,23-JAN-82,1300,,10

[@more@]BLO

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/564597/viewspace-991477/,如需轉載,請註明出處,否則將追究法律責任。

相關文章