在Delphi 7中运行更新/插入查询时,如何解决默认值错误?

亲爱的伏特

我的使用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等普通整数进行测试(使用设置的数字时均有效)。任何帮助将不胜感激。

PS ShowMessage函数用于调试目的,将被删除。

亲爱的伏特

感谢@TLama和@kobik的帮助,我提出了以下解决方案:

整数变量既可以用单引号引起来,也可以用加号括起来,然后放入IntToStr函数中,如下所示:

frmHome.adoqryInLoop.SQL.Text := 'UPDATE Users SET Points = '+IntToStr(iPointsNew)+' WHERE UID = "'+sUID+'"';

要么


可以使用参数化查询(如果出于一致性的原因未对整个程序进行参数化,则有点困难,但是好处多于缺点)。例如:

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;

感谢大家的建议!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章