这段数据库程序,请帮忙修改精减一下,提点意见(50分)

  • 主题发起人 goltiger
  • 开始时间
G

goltiger

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我写的一段程序,
从query1中计算表的一些平均值,
调试通过,但我觉得是否可以精减一下,
能否提高点效率,


procedure TForm1.SpeedButton1Click(Sender: TObject);
var
i,j:integer;
str1,str2,str3,str4,str5,str6,str7,strclass:string;
begin
//平均出钢温度
dm.Query1.Close;
dm.Query1.SQL.Clear;
dm.Query1.SQL.Add('select avg(spitt) as ff from conv where date between :date1 and :date2');
dm.Query1.ParamByName('date1').Value:=strtodate('2003-11-'+combobox2.text);
dm.Query1.ParamByName('date2').Value:=strtodate('2003-11-'+combobox3.text);
try
dm.Query1.Open;
str2:='出钢温度(平均:'+inttostr(round(strtofloat(dm.Query1.FieldValues['ff'])))+')';
groupbox1.Caption:=str2;
except
showmessage('*********');
end;

//计算3个炉子的平均出钢温度
for i:=4 to 6 do
begin
dm.Query1.Close;
dm.Query1.SQL.Clear;
dm.Query1.SQL.Add('select avg(spitt) as ff from conv where date between :date1 and :date2 and conv=:conv1');
dm.Query1.ParamByName('date1').Value :=strtodate('2003-11-'+combobox2.text);
dm.Query1.ParamByName('date2').Value:=strtodate('2003-11-'+combobox3.text);
dm.Query1.ParamByName('conv1').Value:=i;
try
dm.Query1.Open;
str1:=inttostr(round(strtofloat(dm.Query1.FieldValues['ff'])));
if i=4 then
label5.Caption:=str1;
if i=5 then
label6.Caption:=str1;
if i=6 then
label7.Caption:=str1;
except
showmessage('记录超出范围,请确定日期!');
end;
end;

//计算4个班组的平均出钢温度
dm.Query1.Close;
dm.Query1.SQL.Clear;
dm.Query1.SQL.Add('select avg(spitt) as f1 from conv where date between :date1 and :date2 and class=:classno');
dm.Query1.ParamByName('date1').Value :=strtodate('2003-11-'+combobox2.text);
dm.Query1.ParamByName('date2').Value:=strtodate('2003-11-'+combobox3.text);
dm.Query1.ParamByName('classno').Value:='甲';
try
dm.Query1.Open;
str3:=inttostr(round(strtofloat(dm.Query1.FieldValues['f1'])));
label12.Caption:=str3;
except
showmessage('出错,可能是你的日期输入错误!');
end;
dm.Query1.Close;
dm.Query1.SQL.Clear;
dm.Query1.SQL.Add('select avg(spitt) as f1 from conv where date between :date1 and :date2 and class=:classno');
dm.Query1.ParamByName('date1').Value :=strtodate('2003-11-'+combobox2.text);
dm.Query1.ParamByName('date2').Value:=strtodate('2003-11-'+combobox3.text);
dm.Query1.ParamByName('classno').Value:='乙';
try
dm.Query1.Open;
str3:=inttostr(round(strtofloat(dm.Query1.FieldValues['f1'])));
label13.Caption:=str3;
except
showmessage('一个非正常错误!请重新输入日期再试!');
end;
dm.Query1.Close;
dm.Query1.SQL.Clear;
dm.Query1.SQL.Add('select avg(spitt) as f1 from conv where date between :date1 and :date2 and class=:classno');
dm.Query1.ParamByName('date1').Value :=strtodate('2003-11-'+combobox2.text);
dm.Query1.ParamByName('date2').Value:=strtodate('2003-11-'+combobox3.text);
dm.Query1.ParamByName('classno').Value:='丙';
try
dm.Query1.Open;
str3:=inttostr(round(strtofloat(dm.Query1.FieldValues['f1'])));
label14.Caption:=str3;
except
showmessage('天哪,重来吧!');
end;
dm.Query1.Close;
dm.Query1.SQL.Clear;
dm.Query1.SQL.Add('select avg(spitt) as f1 from conv where date between :date1 and :date2 and class=:classno');
dm.Query1.ParamByName('date1').Value :=strtodate('2003-11-'+combobox2.text);
dm.Query1.ParamByName('date2').Value:=strtodate('2003-11-'+combobox3.text);
dm.Query1.ParamByName('classno').Value:='丁';
try
dm.Query1.Open;
str3:=inttostr(round(strtofloat(dm.Query1.FieldValues['f1'])));
label15.Caption:=str3;
except
showmessage('唉,是我太不小心了。你的电脑可能得了非典了!:( ');
end;

//计算总平均出钢温降、吹氩温降
begin
dm.Query1.Close;
dm.Query1.SQL.Clear;
dm.Query1.SQL.Add('select avg(spitt-befort) as ff,avg(befort-aftert) as ee');
dm.Query1.SQL.Add(' from conv where date between :date1 and :date2');
dm.Query1.ParamByName('date1').Value :=strtodate('2003-11-'+combobox2.text);
dm.Query1.ParamByName('date2').Value:=strtodate('2003-11-'+combobox3.text);
try
dm.Query1.Open;
str6:=inttostr(round(strtofloat(dm.Query1.FieldValues['ff'])));
str7:=inttostr(round(strtofloat(dm.Query1.FieldValues['ee'])));
groupbox2.Caption:='温降(出钢:'+str6+',吹氩:'+str7+')';
except
showmessage('记录超出范围,请确定日期!');
end;
end;

//计算出钢温降、吹氩温降
for j:=4 to 6 do
begin
dm.Query1.Close;
dm.Query1.SQL.Clear;
dm.Query1.SQL.Add('select avg(spitt-befort) as ff,avg(befort-aftert) as ee');
dm.Query1.SQL.Add(' from conv where date between :date1 and :date2 and conv=:conv1');
dm.Query1.ParamByName('date1').Value :=strtodate('2003-11-'+combobox2.text);
dm.Query1.ParamByName('date2').Value:=strtodate('2003-11-'+combobox3.text);
dm.Query1.ParamByName('conv1').Value:=j;
try
dm.Query1.Open;
str5:=inttostr(round(strtofloat(dm.Query1.FieldValues['ff'])));
str4:=inttostr(round(strtofloat(dm.Query1.FieldValues['ee'])));
if j=4 then
stringgrid1.Cells[1,1]:=str5;
stringgrid1.Cells[2,1]:=str4;
if j=5 then
stringgrid1.Cells[1,2]:=str5;
stringgrid1.Cells[2,2]:=str4;
if j=6 then
stringgrid1.Cells[1,3]:=str5;
stringgrid1.Cells[2,3]:=str4;
except
showmessage('记录超出范围,请确定日期!');
end;
end;
 
楼主:
没什么可精减的了。
 
一点意见:上面程序中两处多分支的 if 语句可以用 case of ... end 结构来取代,这样免得程序多判断若干次,是不是也可以提高一点点效率呢?呵呵~~~~~
 
楼主的程序里面多次用到dm.Query1.FieldValues['**'];
FieldValues返回的是一个Variant值,如果为空你上面的程序就出错了。
需要转换一下vartostr(dm.Query1.FieldValues['**'])。

你自己可以测试一下为空的情况,我吃过这方面的苦头,以后我从来就不用了。
建议使用Fields.asstring/FieldByName('**').asstring这种形式
 
我的做法:
with dm.Query1 do begin
Close;
SQL.Clear;
SQL.Add('select avg(spitt) as ff from conv where date between :date1 and :date2');
ParamByName('date1').Value:=strtodate('2003-11-'+combobox2.text);
ParamByName('date2').Value:=strtodate('2003-11-'+combobox3.text);
end;
 
为了计算,我 close了 N 次 query1,请问,
能不能只关闭一次,打开一次,(或较少的次数)
就把上述我的程序中的值都能计算出来?
请大家帮帮我!
 
如果是sql server,那建议你使用存储过程,一次性处理完数据然后传回就可以了。
 
其实如果是实现计算功能,用临时表或是存储过程是最好的。像你这样将一个表打开,又关闭,写不同的SQL语句,就不是一个好方法,如果放在一个存储过程中,将事务放在一起处理才会更实用。
 
天啦——楼主下面几句用得有点太过频繁了哦,而且根本就没什么区别:
procedure TForm1.LoadData(SQL,Param,ParamValue:string;ParamCount:Integer);
begin
with dm do
begin
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add(SQL);
Query1.ParamByName('date1').Value :=strtodate('2003-11-'+combobox2.text);
Query1.ParamByName('date2').Value:=strtodate('2003-11-'+combobox3.text);
////////////////////////////////////////////////////////////////////////
if ParamCount=1 then
Query1.ParamByName(param).Value:=ParamValue;
...............................................
哎——还有很多地方可以精简的,眼睛都看花了,暂时就这些吧。
end;
end;
为什么不能把这几句写成过程呢?
把变化得部分用参数得形式传递进去就可以了。
这样可以简化很多代码:


 
呵呵,,不错啊,,[:(]
 
多人接受答案了。
 
顶部