异常和文件

1.异常

异常:在程序运行过程中出现的错误叫异常,包括程序,软件,硬件,网络等系统异常和自定义异常
(1)no_data_found:未找到数据
zero_divide:除数不能为0
to_many_rows:返回结果超出一行
(2)自定义异常
异常变量的定义:

变量名 execute;

异常的处理方式

:抛出异常,捕获异常
抛出异常的方式;
rise 异常变量;
dbms_standard.raise_application_error(异常编码,‘异常信息’)

declare
   --声名一个异常变量(自定义一个异常)
   exc exception;
begin
   raise exc;
end;

begin
  dbms_standard.raise_application_error(-20010,'我的异常');
end;

捕获异常:
declare
   --声名部分
begin
   --代码块部分
   exception
     异常处理部分
     when 异常名称 then
       处理代码;
     when 异常名称 then
       处理代码;
       
     where others then
       处理代码
 
end;

系统异常变量:

sqlcode:获取异常的编码,no_data_found:异常编码时100 不是ora—01403
sqlerrm:获取异常信息

declare
   v varchar2(30);
begin
  select ename into v from emp where empno=7369;
  exception
    when no_data_found then
      dbms_output.put_line(sqlcode);
      dbms_output.put_line(sqlerrm);
  dbms_output.put_line('不管理发不发生异常都要执行的代码'); --只有在发生异常时才会执行
end;

注意: exception和end之间的代码都是异常处理代码

declare
   v varchar2(30);
begin
  select ename into v from emp where empno=7369;
  exception
    when no_data_found then
      dbms_output.put_line(sqlcode);
      dbms_output.put_line(sqlerrm);
  dbms_output.put_line('不管理发不发生异常都要执行的代码'); --只有在发生异常时才会执行
end;

2:异常绑定

将一个异常绑到自定义异常变量上,(绑定之后,可以通过自定义异常变量来捕获该异常)
绑定异常的语法:

declare
异常变量 exception;
pragma exception(异常变量,异常编码);
begin
end

dbms_standard.raise_application_error(自定义异常编码,异常信息);
业务异常或应用异常
自动义异常编码:范围:-20000~-20999

2.文件的读写

directory :目录对象
语法:create directory 目录名称 as ‘文件路径’;
创建目录需要权限,以管理身份运行以下赋权限命令

qgrant create any directory to scott;
create driectory FILEPATE AS 'D/data';
utl_file.file_type:文件类型
utl_file.fopen('目录名称‘,‘文件名',‘读写方式’);打开文件的方法

目录名称:为directory对象名字,必须全部大写
文件名:1.txt

读写方式:

r:表示读,
w“:表示写:如果文件不存在会创建新文件
a:表示追加:如果文件不存在会创建新文件
utl_file.put_line(文件变量,要写入的内容):向文件写入内容
utl_file.get_line(文件变量,字符串变量):从文件读取一行内容,将读取的内容放到变量中,类似fetch into 读取一行后,下次读取会读下一行
utl_file.fclose(文件变量):关闭文件


--写一个代码向一个文件中写入内容
declare
  --声名一个文件变量
  f utl_file.file_type;
begin
  --打开文件
  f:=utl_file.fopen('FILEPATH','test.txt','w');
  --向文件中写入内容
  utl_file.put_line(f,'smith');
  utl_file.put_line(f,'king');
  --关闭文件
  utl_file.fclose(f);
end;

--写一个代码块将emp表中所有员工的姓名写入文件
declare
  --声名文件变量
  f utl_file.file_type;
  
begin
  --打开文件
  f:=utl_file.fopen('FILEPATH','NAMES.TXT','W');
  for v in (select ename from emp) loop
    --向文件中写入员工姓名
    utl_file.put_line(f,v.ename);
  end loop;
  
  --关闭文件
  utl_file.fclose(f);
end;

--文件的追加
declare
   --声名一个文件变量
   f utl_file.file_type;
   
begin
  --打开文件
  f:=utl_file.fopen('FILEPATH','test.txt','a');
  
  utl_file.put_line(f,'张三');
  utl_file.put_line(f,'李四');
  
  --关闭文件
  utl_file.fclose(f);
end;

文件写出

  create directory filepath as 'E:\data';
declare
  f utl_file.file_type;
  v varchar2(100);
begin
  f:=utl_file.fopen('FILEPATH','DEPT.txt','W');
    for n in (select * from dept) loop
      v:=n.deptno||','||n.dname||','||n.loc;
      utl_file.put_line(f,v);
    end loop;
  utl_file.fclose(f);
end;

文件写入到数据库

declare
  F UTL_FILE.FILE_TYPE;
  v dept%rowtype;
  s varchar(100);
begin
  F:=UTL_FILE.FOPEN('FILEPATH','dept.TXT','R');
  loop
    UTL_FILE.GET_LINE(F,S);
   v.deptno:=to_number(REGEXP_SUBSTR(s,'[^,]+',1,1),'9999');
   v.dname:=regexp_substr(s,'[^,]+',1,2);
   v.loc:=regexp_substr(s,'[^,]+',1,3);
   insert into dept1 values(v.deptno,v.dname,v.loc);
  end loop;
     exception
    when no_data_found then
      null;
      utl_file.fclose(f);
end;--exception when no_data_douond then null;是必须加得,否则报错,(终止语句)

null:他是一个空语句,只用来占位,不做任何操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值