为了防止重复添加,想到读数据库中记录的count,如果count>0就表示出现了重复,否则就可以添加,写的代码出问题了,求教!
aq2:=tadoquery.Create(self);
aq2.Connection:=fdbtest.ADOConnection1;
aq2.Close;
aq2.SQL.Clear;
aq2.SQL.Add('select count(*) as cnt from pxda ');
aq2.SQL.add('where Sno=:a and Year=:b and Bigtype=:c and Sectype=:d');
aq2.ExecSQL;
if(aq2.Parameters.ParamByName('cnt').Value=0) then
try
aq1.ExecSQL;
except
end;
其中:a,:b这些参数前面已经附值了的,我按F9run的时候没问题,插入数据的时候aq2.Parameters.ParamByName('cnt').Value=0提示没有cnt这个东东,求高手进来帮我解决问题,谢谢!!!
aq2:=tadoquery.Create(self);
aq2.Connection:=fdbtest.ADOConnection1;
aq2.Close;
aq2.SQL.Clear;
aq2.SQL.Add('select count(*) as cnt from pxda ');
aq2.SQL.add('where Sno=:a and Year=:b and Bigtype=:c and Sectype=:d');
aq2.ExecSQL;
if(aq2.Parameters.ParamByName('cnt').Value=0) then
try
aq1.ExecSQL;
except
end;
其中:a,:b这些参数前面已经附值了的,我按F9run的时候没问题,插入数据的时候aq2.Parameters.ParamByName('cnt').Value=0提示没有cnt这个东东,求高手进来帮我解决问题,谢谢!!!
aq2.Connection:=fdbtest.ADOConnection1;
aq2.Close;
aq2.SQL.Clear;
aq2.SQL.Add('select count(*) as cnt from pxda ');
aq2.SQL.add('where Sno=:a and Year=:b and Bigtype=:c and Sectype=:d');
aq2.open;
if aq2.recordcount > 0 then
try
aq1.ExecSQL;
except
end;
aq2.SQL.add('where Sno=:a and Year=:b and Bigtype=:c and Sectype=:d');
aq2.Parameters.ParamByName('a').Value:= ..;
aq2.Parameters.ParamByName('b').Value:= ..;
aq2.Parameters.ParamByName('c').Value:= ..;
aq2.Parameters.ParamByName('d').Value:= ..;
aq2.open;if aq2.recordcount = 0 then aq1.ExecSQL else showmessage('数据库中已有');
aq1.execsql
except
showmessage('重复')
end;
增删改 语句用execsql方法!
if not exists(select 1 from tablename where field=xxx)
insert into tablename(...) values(...)