0

I have a database application in Delphi and I am trying to open a record and update it. The following is how I do it now:

procedure TWebsiteRecord.UpdateRecord(Website : TWebsite);
var
  SQL : string;
begin
  RecordQuery.SQL.Clear;
  SQL := 'UPDATE website SET Domain=:D, Template=:T, WebHost=:Wh, DomainRegistrar=:Dr, OrderDate=:Od, RenewalDate=:Rd, RenewalCost=:Rc, PaymentMethod=:Pm,' + 'OwnDomainStatus=:OStat, CancellationStatus=:CStat, ReminderStatus=:RStat, WebsiteNotes=:N, FTPUsername=:U1, FTPPassword=:P1, EmailPassword=:P2, PaidForYear=:PStat, CustomerID=:CID WHERE WebsiteID=:WID;'; 
RecordQuery.ParamCheck := True;
RecordQuery.SQL.Add(SQL);
RecordQuery.Params.ParamByName('D').AsString := Website.D;
RecordQuery.Params.ParamByName('T').AsString := Website.T;
RecordQuery.Params.ParamByName('Wh').AsString := Website.Wh;
RecordQuery.Params.ParamByName('Dr').AsString := Website.Dr;
RecordQuery.Params.ParamByName('Od').AsString := Website.Od;
RecordQuery.Params.ParamByName('Rd').AsString := Website.Rd;
RecordQuery.Params.ParamByName('Rc').AsInteger := Website.Rc;
RecordQuery.Params.ParamByName('Pm').AsString := Website.Pm;
RecordQuery.Params.ParamByName('OStat').AsInteger := Website.OStat;
RecordQuery.Params.ParamByName('CStat').AsString := Website.Cstat;
RecordQuery.Params.ParamByName('RStat').AsString := Website.Rstat;
RecordQuery.Params.ParamByName('N').AsString := Website.N;
RecordQuery.Params.ParamByName('U1').AsString := Website.U1;
RecordQuery.Params.ParamByName('P1').AsString := Website.P1;
RecordQuery.Params.ParamByName('P2').AsString := Website.P2;
RecordQuery.Params.ParamByName('PStat').AsInteger := Website.PStat;
RecordQuery.Params.ParamByName('CID').AsInteger := Website.CID;
RecordQuery.Params.ParamByName('WID').AsInteger := Website.WID;
RecordQuery.ExecSQL;
end;

and

procedure TWebsiteRecord.SaveBtnClick(Sender: TObject);
var
Website : TWebsite;
begin
if Validate then
begin
  Website.D := DomainEdit.Text;
  Website.T := TemplateEdit.Text;
  Website.Wh := WebHostEdit.Text;
  Website.Dr := DomainRegEdit.Text;
  Website.Od := GetSQLDate(Date1Edit.Text);
  Website.Rd := GetSQLDate(Date2Edit.Text);
  if Website.Rd = '' then Website.Rd := '70/01/19';                         
  if CostEdit.Text = '' then website.Rc := 0
  else Website.Rc := strtoint(CostEdit.Text);
  Website.Pm := GetPaymentMethod(PaymentMethodCombo.ItemIndex);
  Website.OStat := integer(OwnDomainCheck.Checked);
  if PendingCheck.Checked then Website.Cstat := 'P'
  else if CancelledCheck.Checked then Website.Cstat := 'C'
  else Website.Cstat := 'A';
  Website.Rstat := GetSent(ReminderStatusCombo.ItemIndex);
  Website.N := NotesMemo.Text;
  Website.U1 := FTPUserEdit.Text;
  Website.P1 := FTPPassEdit.Text;
  Website.P2 := EmailPassEdit.Text;
  Website.PStat := integer(PaidCheck.Checked);
  Website.CID := strtoint(CustIDEdit.Text);

  UpdateRecord(Website);

  messagedlg('Website successfully updated, You will now be returned to the website table',mtinformation,[mbOK],0);
  WebsiteTable.WebsiteCDS.Refresh;
  Free;
end;
end;

There are no errors caused when this is executed, but the record is not updated and remains exactly the same as it was before. Does anyone know this problem? IIf so what can i do. I can provide more code if it is needed. Thanks in advance

ain
  • 22,394
  • 3
  • 54
  • 74
TobyFox1997
  • 71
  • 1
  • 5

1 Answers1

0

As I can see your main problem is that you don't know what exact query is executed by the server.

It may seem a bit old fashioned, but if I were you I'd use the plain old FORMAT() function to debug the query and see what exactly is passed to the server. In this case your code will be something like that:

SQL := FORMAT('UPDATE website SET Domain="%s", Template="%s", WebHost="%s", DomainRegistrar="%s", OrderDate="%s", RenewalDate="%s", RenewalCost=%d, PaymentMethod="%s",' +
  'OwnDomainStatus=%d, CancellationStatus="%s", ReminderStatus="%s", WebsiteNotes="%s", FTPUsername="%s", FTPPassword="%s", EmailPassword="%s", PaidForYear=%d, CustomerID=%d WHERE WebsiteID=%d',
  [
    RecordQuery.Params.ParamByName('D').AsString,
    RecordQuery.Params.ParamByName('T').AsString,
    RecordQuery.Params.ParamByName('Wh').AsString,
    RecordQuery.Params.ParamByName('Dr').AsString,
    RecordQuery.Params.ParamByName('Od').AsString,
    RecordQuery.Params.ParamByName('Rd').AsString,
    RecordQuery.Params.ParamByName('Rc').AsInteger,
    RecordQuery.Params.ParamByName('Pm').AsString,
    RecordQuery.Params.ParamByName('OStat').AsInteger,
    RecordQuery.Params.ParamByName('CStat').AsString,
    RecordQuery.Params.ParamByName('RStat').AsString,
    RecordQuery.Params.ParamByName('N').AsString,
    RecordQuery.Params.ParamByName('U1').AsString,
    RecordQuery.Params.ParamByName('P1').AsString,
    RecordQuery.Params.ParamByName('P2').AsString,
    RecordQuery.Params.ParamByName('PStat').AsInteger,
    RecordQuery.Params.ParamByName('CID').AsInteger,
    RecordQuery.Params.ParamByName('WID').AsInteger
  ]);

and you could view the query, log it in a text file or just copy it and paste it somewhere to view it's execution like mysql command prompt or phpmyadmin.

If you have access to server's log file this is not needed - just open it and see the query inside.

Of cource once the query works as expected you may use it with parameters. And depending on what exactly connection type you are using there may be other (and faster) ways to get the actual query, e.g. this question

Community
  • 1
  • 1
LHristov
  • 1,103
  • 7
  • 16