如何捕捉插入记录时,主键或唯一索引重复引发的异常?建议者有分。

feng93017 2002-11-25 12:33:05
如何捕捉插入记录时,主键或唯一索引重复引发的异常?
...全文
961 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
norxi 2002-12-01
  • 打赏
  • 举报
回复
我贴的是delphi的demo的程序
要学好delphi,先要学好里面的demo!
:)
zhang21cnboy 2002-11-29
  • 打赏
  • 举报
回复
有两种思想,第一,事前保护性的,就是说再用户输入的时候马上做出反应
这样的好处是对用户的输入有好处,不好的地方,速度!!!!!!!
第二,事后保护,就是当用户提交以后,发现错误,然后高数用户错了!
这样的话,速度上没有问题,不过对用户来说,不怎么人道,刚才输入的数据全部死翘了!

如果你不怕麻烦,你可以把两者巧妙的结合!:)
vmao 2002-11-29
  • 打赏
  • 举报
回复
norxi(\(^o^)/\(^o^)/) 的方法很好!

ado就要拦截ado的ERRORS对象!这个方法是不行的!
rocktan 2002-11-29
  • 打赏
  • 举报
回复
EDBEngineError好象在ADO中用不了:
Indicates the error code returned by the BDE.
ADOtable.postError中如何搞定?
norxi 2002-11-27
  • 打赏
  • 举报
回复
数据库异常状态:
A complete listing of the database errorcodes is found in the
DBIErrs.Int file in the Delphi/Doc directory or in the IDAPI.h
file in the Borland Database Engine.

Database errors are defined by category and code. Here's a sample:

{ ERRCAT_INTEGRITY }

ERRCODE_KEYVIOL = 1; { Key violation }
ERRCODE_MINVALERR = 2; { Min val check failed }
ERRCODE_MAXVALERR = 3; { Max val check failed }
ERRCODE_REQDERR = 4; { Field value required }
ERRCODE_FORIEGNKEYERR = 5; { Master record missing }
ERRCODE_DETAILRECORDSEXIST = 6; { Cannot MODIFY or DELETE this Master record }
ERRCODE_MASTERTBLLEVEL = 7; { Master Table Level is incorrect }
ERRCODE_LOOKUPTABLEERR = 8; { Field value out of lookup tbl range }
ERRCODE_LOOKUPTBLOPENERR = 9; { Lookup Table Open failed }
ERRCODE_DETAILTBLOPENERR = 10; { 0x0a Detail Table Open failed }
ERRCODE_MASTERTBLOPENERR = 11; { 0x0b Master Table Open failed }
ERRCODE_FIELDISBLANK = 12; { 0x0c Field is blank }


The constant for the base category is added to these constants to represent
a unique DBI errorcode;

DBIERR_KEYVIOL = (ERRBASE_INTEGRITY + ERRCODE_KEYVIOL);
DBIERR_REQDERR = (ERRBASE_INTEGRITY + ERRCODE_REQDERR);
DBIERR_DETAILRECORDSEXIST = (ERRBASE_INTEGRITY + ERRCODE_DETAILRECORDSEXIST);
DBIERR_FORIEGNKEYERR = (ERRBASE_INTEGRITY + ERRCODE_FORIEGNKEYERR);

The ERRBASE_INTEGRITY value is $2600 (Hex 2600) or 9728 decimal.
Thus, for example, the errorcode for keyviol is 9729
for master with details is 9734.


开头加上:
const
eKeyViol = 9729;
eRequiredFieldMissing = 9732;
eForeignKey = 9733;
eDetailsExist = 9734;

implementation

1、主从表删主表(无级联删除情况)
procedure Tdatamodule1.table1DeleteError(Dataset:Tdataset;E:EDatabaseError;var Action:TdataAction);
begin
if (E is EDBEngineError) then
if (E as EDBEngineError).Error[0].Errorcode=eDetailsExist then
begin
messageDLG('请先删除从表的相关的记录!',mtWarning,[mbOK],0);
abort;
end;
end;
2、主关键字重复
procedure Tdatamodule1.table1PostError(Dataset:Tdataset;E:EDatabaseError;var Action:TdataAction);
begin
if (E is EDBEngineError) then
if (E as EDBEngineError).Error[0].Errorcode=eKeyViol then
begin
messageDLG('主关键字重复!已经存在该编号!',mtWarning,[mbOK],0);
abort;
end;
end;
3、关键字重复和关键字为空的情况
procedure Tdatamodule1.table1PostError(Dataset:Tdataset;E:EDatabaseError;var Action:TdataAction);
var Error:integer;
begin
if (E is EDBEngineError) then
begin
Error:= (E as EDBEngineError).Errors[0].Errorcode;
case Error of
eRequiredFieldMissing: //关键字为空
begin
messageDLG('主关键字不能为空!',mtWarning,[mbOK],0);
Abort;
end;
eKeyViol: //关键字已经存在
begin
messageDLG('主关键字重复!已经存在该编号!',mtWarning,[mbOK],0);
Abort;
end;
end;
end;
end;
zhangqiufk 2002-11-27
  • 打赏
  • 举报
回复
上方法不行,不同数据库“重复引发的异常信息”是不同的.我的方法是
BEFOREPOST时检测是否主键冲突。
rocktan 2002-11-25
  • 打赏
  • 举报
回复
try
insert...
except
on E:Exception do
if Pchar(e.message) = 重复引发的异常信息 then
showmessage('记录字段不能重复')
end;
end;
charlish 2002-11-25
  • 打赏
  • 举报
回复
try
...
except
on E:Exception do
showmessage(pchar(E.Message));
end

2,498

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧