程序功能是先查询记录是否存在,若存在,则更新记录;若不存在,则添加记录
目前存在的问题:当系统判断记录不存在,插入新记录没有问题
当判断记录存在,更新记录出问题
代码如下:procedure Tfinput_CW.btnClearClick(Sender: TObject);
begin
cmbFactory.Text:= '';
edtAmount.Text:= '';
edtQuantity.Text:= '';end;procedure Tfinput_CW.Button8Click(Sender: TObject);
begin
FexchangeRate.ShowModal;
end; //录入进出口费用,若存在记录,则更新,若不存在,则添加 记录
procedure Tfinput_CW.btnSaveClick(Sender: TObject);var count : integer ;
year : string ;
period :string ;
factory :string;
Amount : integer;
ProductNumber:integer;
perAmount: single ;begin
year := cmbyear.Text;
period := cmbPeriod.Text;
factory := cmbfactory.Text;
amount := strToint(edtAmount.Text);
ProductNumber := strToint(edtQuantity.Text);
peramount := amount/productnumber; with datamd.adoImport do //查找符合条件的记录数,用count记数
begin
close;
sql.Clear;
sql.Text := 'select count(*) from t_importAndExport where fyear = ''' + year + ''' and fperiod = ''' + period + ' ''and ffactory =''' + factory + '''';
showmessage(sql.text);
open;
count := Fields[0].AsInteger;
end; if count = 0 then //判断是否存在记录
begin
with datamd.adoImport do
begin
close;
sql.Clear;
sql.Text := 'insert into t_importAndExport values(:year,:period,:factory,:amount,:productNumber,:perAmount)'; parameters.ParamByName('year').Value := year;
parameters.ParamByName('period').Value := period;
parameters.ParamByName('factory').Value := factory;
parameters.ParamByName('amount').Value := amount;
parameters.ParamByName('productNumber').Value := productNumber;
parameters.ParamByName('peramount').Value := peramount; showmessage(sql.text);
execSQL; end;
end else
begin
with datamd.adoImport do //存在记录则更新
begin
close;
sql.Clear;
sql.Text := 'update t_importAndExport set fAmount =' + inttostr(amount) + ',fproductNumber = ' + inttostr(productnumber) + ',fperAmount = '+ FloatToStr(perAmount) +' where fyear = ''' + year + ''' and fperiod = ''' + period + ' ''and ffactory =''' + factory + '''';
showmessage(sql.text);
open;
end;
end;
end;
先谢谢大家了,有描述不清的请您提出来
目前存在的问题:当系统判断记录不存在,插入新记录没有问题
当判断记录存在,更新记录出问题
代码如下:procedure Tfinput_CW.btnClearClick(Sender: TObject);
begin
cmbFactory.Text:= '';
edtAmount.Text:= '';
edtQuantity.Text:= '';end;procedure Tfinput_CW.Button8Click(Sender: TObject);
begin
FexchangeRate.ShowModal;
end; //录入进出口费用,若存在记录,则更新,若不存在,则添加 记录
procedure Tfinput_CW.btnSaveClick(Sender: TObject);var count : integer ;
year : string ;
period :string ;
factory :string;
Amount : integer;
ProductNumber:integer;
perAmount: single ;begin
year := cmbyear.Text;
period := cmbPeriod.Text;
factory := cmbfactory.Text;
amount := strToint(edtAmount.Text);
ProductNumber := strToint(edtQuantity.Text);
peramount := amount/productnumber; with datamd.adoImport do //查找符合条件的记录数,用count记数
begin
close;
sql.Clear;
sql.Text := 'select count(*) from t_importAndExport where fyear = ''' + year + ''' and fperiod = ''' + period + ' ''and ffactory =''' + factory + '''';
showmessage(sql.text);
open;
count := Fields[0].AsInteger;
end; if count = 0 then //判断是否存在记录
begin
with datamd.adoImport do
begin
close;
sql.Clear;
sql.Text := 'insert into t_importAndExport values(:year,:period,:factory,:amount,:productNumber,:perAmount)'; parameters.ParamByName('year').Value := year;
parameters.ParamByName('period').Value := period;
parameters.ParamByName('factory').Value := factory;
parameters.ParamByName('amount').Value := amount;
parameters.ParamByName('productNumber').Value := productNumber;
parameters.ParamByName('peramount').Value := peramount; showmessage(sql.text);
execSQL; end;
end else
begin
with datamd.adoImport do //存在记录则更新
begin
close;
sql.Clear;
sql.Text := 'update t_importAndExport set fAmount =' + inttostr(amount) + ',fproductNumber = ' + inttostr(productnumber) + ',fperAmount = '+ FloatToStr(perAmount) +' where fyear = ''' + year + ''' and fperiod = ''' + period + ' ''and ffactory =''' + factory + '''';
showmessage(sql.text);
open;
end;
end;
end;
先谢谢大家了,有描述不清的请您提出来
begin
close;
sql.Clear;
sql.Text := 'update t_importAndExport set fAmount =' + inttostr(amount) + ',fproductNumber = ' + inttostr(productnumber) + ',fperAmount = '+ FloatToStr(perAmount) +' where fyear = ''' + year + ''' and fperiod = ''' + period + ' ''and ffactory =''' + factory + '''';
showmessage(sql.text);
open;//这里也应该用execSQL来执行修改就行了,不是select 语句,不能打开.
end;
end;
Open:返回结果集,用于Select等操作
sql.Text := 'select count(*) from t_importAndExport where fyear = ''' + year + ''' and fperiod = 可以这样写
sql.Text := 'select count(*) from t_importAndExport where fyear = ' + QuotedStr(year) + ' and fperiod =