我收到此错误消息:
列出索引越界(1)
尝试从我的数据库中选择信息时。我正在使用Delphi XE7,MySQL 6.2,FDConnection和FDQuery。我的代码是:
Procedure TAppointmentForm.GetTreatPrice;
Begin
TreatPriceQuery.SQL.Text:= 'Select Cost from Treatment where TreatName = '+quotedstr(Treatment)+'';
TreatPriceQuery.Open;
TreatPrice:= TreatPriceQuery.FieldByName('Cost').AsInteger;
End;
我正在使用CheckBoxList获取Treatment
。我的代码是:
Procedure TAppointmentForm.GetAppCost;
Var
Count: Integer;
begin
for Count := 0 to (Count1-1) do
Begin
if TreatmentCheckListBox.State[Count] = cbChecked then
Begin
Treatment:= TreatmentCheckListBox.Items.Strings[Count];
GetTreatPrice;
AppCost:= AppCost + TreatPrice;
End
Else
AppCost:= AppCost;
End;
end;
答案 0 :(得分:6)
您的代码过于复杂。您可以使用Checked
的{{1}}属性,并在访问内容中的项目时完全删除TCheckListBox
(Strings
是Strings
的默认属性)。此外,您应该在循环中使用Items
Count
。
Items
此外,停止连接SQL的文本,并使用参数化查询,以提高效率和防止SQL注入。
Procedure TAppointmentForm.GetAppCost;
Var
Idx: Integer;
begin
for Idx := 0 to TreatmentCheckListBox.Items.Count - 1 do
Begin
if TreatmentCheckListBox.Checked[Idx] then
Begin
Treatment:= TreatmentCheckListBox.Items[Idx];
GetTreatPrice;
AppCost:= AppCost + TreatPrice;
End;
// The next two lines are a non operation. Assigning a
// variable to itself does nothing. Remove them entirely
// Else
// AppCost:= AppCost;
End;
end;
我同意@ Remy在您的问题评论中的内容。您应该传递参数而不是使用全局变量。
Procedure TAppointmentForm.GetTreatPrice;
Begin
TreatPriceQuery.SQL.Text:= 'Select Cost from Treatment where TreatName = :TreatName';
TreatPriceQuery.ParamByName('TreatName').AsString := Treatment;
TreatPriceQuery.Open;
TreatPrice:= TreatPriceQuery.FieldByName('Cost').AsInteger;
End;