我在使用Microsoft Jet引擎和Microsoft Access(* .mdb)数据库的应用程序中继续在Delphi 7中出错。我通过TADOQuery组件建立连接。错误显示'参数iPointsNew没有默认值',只有在更新/插入查询中使用整数变量时才会发生:
frmHome.adoqryInLoop.SQL.Text := 'UPDATE Users SET Points = iPointsNew WHERE UID = "'+sUID+'"';
事件处理程序的代码如下:
procedure TfrmAdmin.bmbSubmitClick(Sender: TObject);
var
sScore, sEID, sPrediction, sUID : String;
iRecordCount, x, iPos, iLength, iActual, iPoints, iPointsNew : Integer;
rPrediction : Real;
begin
// Assign values to variables
sScore := IntToStr(sedSuthies.Value) + '-' + IntToStr(sedOpponent.Value);
iActual := sedSuthies.Value + sedOpponent.Value;
sEID := frmHome.arrEID[lstEvents.ItemIndex];
// Update the score for the event in the database
frmHome.adoqryMain.Active := False;
frmHome.adoqryMain.SQL.Clear;
frmHome.adoqryMain.SQL.Text := 'UPDATE Events SET Score = "'+sScore+'",Complete = True WHERE EID = "'+sEID+'" ';
frmHome.adoqryMain.ExecSQL;
frmHome.adoqryMain.SQL.Text := 'SELECT * FROM Predictions WHERE EID = "'+sEID+'" ';
frmHome.adoqryMain.Open;
iRecordCount := frmHome.adoqryMain.RecordCount;
//Assign points to users for all the predictions
for x := 0 to (iRecordCount - 1) do begin
sUID := frmHome.adoqryMain.Fields[1].AsString;
sPrediction := frmHome.adoqryMain.Fields[4].AsString;
iPos := Pos('-',sPrediction) - 1;
iLength := Length(sPrediction) - iPos;
ShowMessage('1');
if ((sedSuthies.Value >= sedOpponent.Value) AND (StrToFloat(Copy(sPrediction, 1, iPos)) >= StrToFloat(Copy(sPrediction, iPos + 2, iLength + 1)))) OR ((sedSuthies.Value < sedOpponent.Value) AND (StrToFloat(Copy(sPrediction, 1, iPos)) < StrToFloat(Copy(sPrediction, iPos + 2, iLength + 1)))) then begin
rPrediction := StrToFloat(Copy(sPrediction, iPos + 2, iLength + 1)) + StrToFloat(Copy(sPrediction, 1, iPos));
if rPrediction >= iActual then
rPrediction := rPrediction - iActual
else
rPrediction := iActual - rPrediction;
iPoints := Round(10 * (1 - (rPrediction / iActual)));
end
else
iPoints := 0;
ShowMessage('2');
frmHome.adoqryInLoop.Close;
frmHome.adoqryInLoop.SQL.Text := 'SELECT * FROM Users WHERE UID ="'+sUID+'"';
frmHome.adoqryInLoop.Open;
iPointsNew := frmHome.adoqryInLoop.Fields[10].AsInteger + iPoints;
ShowMessage(IntToStr(iPointsNew));
frmHome.adoqryInLoop.Close;
frmHome.adoqryInLoop.SQL.Text := 'UPDATE Users SET Points = iPointsNew WHERE UID = "'+sUID+'"';
frmHome.adoqryInLoop.ExecSQL;
frmHome.adoqryInLoop.Close;
frmHome.adoqryInLoop.SQL.Text := 'UPDATE Predictions SET Complete = True WHERE UID = "'+sUID+'" AND EID = "'+sEID+'" ';
frmHome.adoqryInLoop.ExecSQL;
ShowMessage('3');
ShowMessage(IntToStr(iPoints));
frmHome.adoqryMain.Next;
end;
ShowMessage('Score succefully submitted!');
frmHome.UpdateEventLists;
end;
仅在尝试使用整数变量时才会出现此错误。我尝试了很多解决方案,例如使用引号,加上'甚至用普通整数测试,例如10(使用固定数字时有效)。任何帮助将不胜感激。
P.S。 ShowMessage函数用于调试目的,将被删除。
答案 0 :(得分:5)
感谢@TLama和@kobik的帮助,我想出了一个如下解决方案:
整数变量可以用单引号括起来加上&#39;并输入这样的IntToStr函数:
frmHome.adoqryInLoop.SQL.Text := 'UPDATE Users SET Points = '+IntToStr(iPointsNew)+' WHERE UID = "'+sUID+'"';
OR
可以使用参数化查询(如果整个程序没有参数化以保持一致性,有点困难,但好处大于缺点)。例如:
frmHome.adoqryInLoop.SQL.Text := 'UPDATE Users SET Points = :points WHERE UID = :uid';
frmHome.adoqryInLoop.Params.ParamByName('points').Value := iPointsNew;
frmHome.adoqryInLoop.Params.ParamByName('uid').Value := sUID;
frmHome.adoqryInLoop.ExecSQL;
感谢所有人的建议!