你要不就写SQL ,要不就用 Try Except 捕捉错误SQL 可以如下写法,如果没有重复的主键则Insert: if not exists(select MainID from mainTable where MainID=@mainID) Insert Into MainTABLE (mainID,...)values(@mainID,...)
suton(suton) 的方法也是很常用的方法,推荐在前台程序内写而不通过数据库反馈
query1.close; query1.sql.clear; query1.sql.add('select * from table1 where id=:id1'); query1.parambyname('id').asstring:=edit1.text; query1.open; if query1.recordcount>0 then begin showmessage('数据库中有重复记录'); abort; end;
From test
Where 主键=新输入记录的主键
然后判断RecordCount是否大于0
if not exists(select MainID from mainTable where MainID=@mainID) Insert Into MainTABLE (mainID,...)values(@mainID,...)
query1.sql.clear;
query1.sql.add('select * from table1 where id=:id1');
query1.parambyname('id').asstring:=edit1.text;
query1.open;
if query1.recordcount>0 then
begin
showmessage('数据库中有重复记录');
abort;
end;