简体   繁体   English

使用UPDATE的Delphi SQL集成

[英]Delphi SQL integration using UPDATE

I have a database application in Delphi and I am trying to open a record and update it. 我在Delphi中有一个数据库应用程序,我正在尝试打开一条记录并更新它。 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. IIf,那我该怎么办。 I can provide more code if it is needed. 如果需要,我可以提供更多代码。 Thanks in advance 提前致谢

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. 它似乎有点过时,但是如果我是我,那么我将使用普通的FORMAT()函数调试查询,并查看传递给服务器的确切信息。 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. 您可以查看查询,将其记录在文本文件中,也可以将其复制并粘贴到某处以查看其执行,例如mysql命令提示符或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, eg this question 并且取决于您使用的连接类型是什么,可能还有其他(更快)的方法来获取实际的查询,例如, 这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM