ITPub博客

首页 > IT职业 > IT生活 > BLOB方面的学习笔记

BLOB方面的学习笔记

原创 IT生活 作者:jixuewen 时间:2007-12-11 15:06:46 0 删除 编辑

自己的学习笔记,可以将一个片什么 的搞到数据库中,也可以将一个电影从数据库中导出来。

当然对于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/,如需转载,请注明出处,否则将追究法律责任。

上一篇: Utl_file文章使用
请登录后发表评论 登录
全部评论
  • 博文量
    95
  • 访问量
    1994324