Oracle之UTL_FILE 包用法例子

A、说明

UTL_FILE是oracle提供的一个标准的工具包,用来读写文件使用。

B、创建路径

包UTL_FILE用于R.W.操作系统的文件,首先创建路径(Directory)并授权。ORACLE目录的作用就是让ORACLE数据库和操作系统之前进行文件的交互。
为了创建目录,必须具有DBA角色或者赋予了CREATE ANY DIRECTORY权限。如果普通用户被赋予了CREATE ANY DIRECTORY权限,那么用户就自动具备目录的R和W权限.
1.以管理员用户登陆(在服务器端Oracle执行)
--创建(以system用户登录数据库)
create or replace directory DIR as 'c:\tmp\DIR'; 

--路径授权,添加对路径读、写权限
grant read,write on directory DIR to public;
 
--utl_file包授权,添加执行权限
GRANT EXECUTE ON utl_file TO public;

--检查目录是否成功创建
select * FROM all_directories dir WHERE dir.DIRECTORY_NAME = 'DIR';
2.切换成用户登录
--检查目录是否成功创建
select * FROM all_directories dir WHERE dir.DIRECTORY_NAME = 'DIR';

C、题目(利用oracle的utl_file包将.hyou文件的数据提取到t_tmp表中)

文件a.hyou:

TIME=2022/01/02 08:41:30
TYPE=M
</Mau
Mau
abcd|C|0
|||*|
今天|真好|哇|123
Ll|平时|
||||||
|||*|
/Mau>
实例
create or replace procedure fileutL(v_filename in varchar2) AS
  l_loc         all_directories.directory_name%TYPE := 'DIR';
  l_file        utl_file.file_type;
  l_file_exsits BOOLEAN;
  l_file_length NUMBER;
  l_block_size  BINARY_INTEGER;
  --l_buffer      VARCHAR2(1024);

  v_text           varchar2(100);      --存放文本
  v_name1          t_tmp.name1%type;
  v_name2          t_tmp.name2%type;
  v_name3          t_tmp.name3%type;
  v_name4          t_tmp.name4%type;
  v_name5          t_tmp.name5%type;
  
  v_firstlocation  number;
  v_secondlocation number;
  v_thirtylocation number;
  v_total          number;
 -- i                int :=1;
 -- n                int :=2;
  m                Integer;
BEGIN
  utl_file.fgetattr(location    => l_loc,
                    filename    => v_filename,--'a.hyou',
                    fexists     => l_file_exsits,
                    file_length => l_file_length,
                    block_size  => l_block_size);
 IF l_file_exsits THEN

  l_file := utl_file.fopen(location  => l_loc,
                           filename  => v_filename,--'a.hyou',
                           open_mode => 'R');

  --dbms_output.put_line('file exsits');
  --dbms_output.put_line('file length:' || l_file_length);      
  --dbms_output.put_line('block sieze :' || l_block_size); 
  
    LOOP
    begin
      utl_file.get_line(file    =>l_file,
                        buffer  =>v_text); --取数据
    exception
      when no_data_found then
        DBMS_Output.Put_Line('没有数据');
        --exit;
    end;
    if v_text='</Mau' then 
      m:=0;
    end if;
    --dbms_output.put_line('after  get_line: current position is ' || utl_file.fgetpos(file => l_file));  
    if m>=1  then 
      v_firstlocation  := instr(v_text, '|', 1,1);--第一次出现‘|’
      v_secondlocation := instr(v_text, '|', 1,2);--第二次出现‘|’
      v_thirtylocation := instr(v_text, '|', 1,3);--第三次出现‘|’
      v_total          :=length(v_text);--字符串长度
      --if  v_firstlocation=10 and v_secondlocation=12 then
      if m= 2 then
        v_name1       := substr(v_text, 1, v_firstlocation - 1);
        v_name2		  := substr(v_text, v_firstlocation + 1, v_secondlocation - v_firstlocation - 1);
        DBMS_Output.Put_Line('v_name1:'||v_name1);
        DBMS_Output.Put_Line('v_name2:'||v_name2);
      elsif m=4 then
       v_name4:=substr(v_text, v_thirtylocation + 1, v_total-v_thirtylocation);
       v_name5:= substr(v_text, 1, v_firstlocation - 1);
       DBMS_Output.Put_Line('v_name4:'||v_name4);
       DBMS_Output.Put_Line('v_name5:'||v_name5);
      elsif m=5 then
       v_name3:=substr(v_text, v_firstlocation + 1, v_secondlocation - v_firstlocation - 1);
       DBMS_Output.Put_Line('v_name3:'||v_name3); 
      end if; 
      
      if v_text='/Mau>' then
        --插入数据库操作
        insert into t_tmp(name1,name2,name3,name4,name5)  values(v_name1,v_name2,v_name3,v_name4,v_name5);
        commit;
        exit;
      end if;  
    
    end if;
   
   --  i:=i+1;
     
     if m>=0 then
       m:=m+1;
     end if;
   
 END LOOP;
 utl_file.fclose(l_file);
 dbms_output.put_line('退出'); 
END IF;


exception
  when others then
   DBMS_Output.Put_Line('数据时出错!');
utl_file.fclose(l_file);
END fileutL;

修订:

出现中文乱码,服务器和客户端编码一致,文件编码为UTF-8 ,将文件另存为编码gb2312 的文件保存,OK

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摘星_goddess

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值