您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL5的异常处理(1)

2025/6/25 20:45:24发布24次查看
1. sample problem: log of failures 问题样例:故障记录 当insert失败时,我希望能将其记录在日志文件中我们用来展示出错处理的问题样例是很普通的。我希望得到错误的记录。当insert失败时,我想在另一个文件中记下这些错误的信息,例如出错时间,出错原因
1. sample problem: log of failures 问题样例:故障记录
当insert失败时,我希望能将其记录在日志文件中我们用来展示出错处理的问题样例是很普通的。我希望得到错误的记录。当insert失败时,我想在另一个文件中记下这些错误的信息,例如出错时间,出错原因等。我对插入特别感兴趣的原因是它将违反外键关联的约束
2. sample problem: log of failures (2)
mysql> create table t2
1 int, primary key (s1))
engine=innodb;//
mysql> create table t3 (s1 int, key (s1),
foreign key (s1) references t2 (s1))
engine=innodb;//
mysql> insert into t3 values (5);//
...
error 1216 (23000): cannot add or update a child row: a foreign key
constraint fails(这里显示的是系统的出错信息)
我开始要创建一个主键表,以及一个外键表。我们使用的是innodb,因此外键关联检查是打开的。然后当我向外键表中插入非主键表中的值时,动作将会失败。当然这种条件下可以很快找到错误号1216。
3. sample problem: log of failures
create table error_log (error_message
char(80))//
下一步就是建立一个在做插入动作出错时存储错误的表。
4. sample problem: log of errors
create procedure p22 (parameter1 int)
begin
declare exit handler for 1216
insert into error_log values
(concat('time: ',current_date,
'. foreign key reference failure for
value = ',parameter1));
insert into t3 values (parameter1);
end;//
上面就是我们的程序。这里的第一个语句declare exit handler是用来处理异常的。意思是如果错误1215发生了,这个程序将会在错误记录表中插入一行。exit意思是当动作成功提交后退出这个复合语句。
5. sample problem: log of errors
call p22 (5) //
调用这个存储过程会失败,这很正常,因为5值并没有在主键表中出现。但是没有错误信息返回因为出错处理已经包含在过程中了。t3表中没有增加任何东西,但是error_log表中记录下了一些信息,这就告诉我们insert into table t3动作失败。
declare handler syntax 声明异常处理的语法
declare
{ exit | continue }
handler for
{ error-number | { sqlstate error-string } | condition }
sql statement
上面就是错误处理的用法,也就是一段当程序出错后自动触发的代码。mysql允许两种处理器,一种是exit处理,我们刚才所用的就是这种。另一种就是我们将要演示的,continue处理,它跟exit处理类似,不同在于它执行后,原主程序仍然继续运行,那么这个复合语句就没有出口了。
1. declare continue handler example continue处理例子
create table t4 (s1 int,primary key(s1));//
create procedure p23 ()
begin
declare continue handler
for sqlstate '23000' set @x2 = 1;
set @x = 1;
insert into t4 values (1);
set @x = 2;
insert into t4 values (1);
set @x = 3;
end;//
这是mysql参考手册上的continue处理的例子,这个例子十分好,所以我把它拷贝到这里。通过这个例子我们可以看出continue处理是如何工作的。
2. declare continue handler声明continue异常处理
create table t4 (s1 int,primary key(s1));//
create procedure p23 ()
begin
declare continue handler
for sqlstate '23000' set @x2 = 1; set @x = 1;
insert into t4 values (1);
set @x = 2;
insert into t4 values (1);
set @x = 3;
end;//
这次我将为sqlstate值定义一个处理程序。还记得前面我们使用的mysql错误代码1216吗?事实上这里的23000sqlstate是更常用的,当外键约束出错或主键约束出错就被调用了。
3. declare continue handler
create table t4 (s1 int,primary key(s1));//
create procedure p23 ()
begin
declare continue handler
for sqlstate '23000' set @x2 = 1;
set @x = 1; insert into t4 values (1);
set @x = 2;
insert into t4 values (1);
set @x = 3;
end;//
这个存储过程的第一个执行的语句是set @x = 1。
4. declare continue handler example
create table t4 (s1 int,primary key(s1));//
create procedure p23 ()
begin
declare continue handler
for sqlstate '23000' set @x2 = 1;
set @x = 1;
insert into t4 values (1);
set @x = 2;
insert into t4 values (1); set @x = 3;
end;//
运行后值1被插入到主键表中。
5. declare continue handler
create table t4 (s1 int,primary key(s1));//
create procedure p23 ()
begin
declare continue handler
for sqlstate '23000' set @x2 = 1;
set @x = 1;
insert into t4 values (1);
set @x = 2; insert into t4 values (1);
set @x = 3;
end;//
然后@x的值变为2。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product