How to include data from a multiline query in a memo using Delphi?

0

Good afternoon.

I work on a legacy system and need to put information from a query into a single memo object.

Code follows.

MyQuery.Close;

with MyQuery.SQL do
begin
     clear;
     add(' select TOP 8 nuentrada, dstpleitu, vlleitura, dshint, dtleitura, hhleitura ');
     add(' from ssdentsinaisvitais inner join ssttplei on ssdentsinaisvitais.cdleitura = ssttplei.cdleitura ');
     add(' where nuentrada = ' + lblnuentrada.Caption);
     add(' order by dtleitura desc, hhleitura desc ');
 end;

 MyQuery.Open;

 while MyQuery.Eof do
 begin
      mmdsexamefisico.Text := mmdsexamefisico.Text + MyQuery.DataSource.DataSet.FieldByName('dstpleitu').AsString + ': ';
      mmdsexamefisico.Text := mmdsexamefisico.Text + MyQuery.DataSource.DataSet.FieldByName('vlleitura').AsString + #13#10;
 end; 

A syntax error occurs next to the word order.

Can you detect the error?

The% formed% is as follows:

SELECT TOP 8 nuentrada,
             dstpleitu,
             vlleitura,
             dshint,
             dtleitura,
             hhleitura
  FROM ssdentsinaisvitais
       INNER JOIN ssttplei ON ssdentsinaisvitais.cdleitura = ssttplei.cdleitura 
 WHERE nuentrada = 350000701778
 ORDER BY dtleitura DESC,
       hhleitura DESC
    
asked by anonymous 03.12.2018 / 18:10

2 answers

0

You are using data type BIGINT instead of data type INT , because the maximum value of INT is 2.147.483.647 , you can consult the online documents [1] here to see. Also consider using parameters to avoid these errors and SQL Injection [2] .

♦ If you are using TFDQuery :

Const
  Num: Largeint = 350000701778;
Var
  FDQuery: TFDQuery;
begin
  FDQuery := TFDQuery.Create(Nil);
  try
    with FDQuery do
      begin
        Connection:= FDConnection1;
        SQL.Text:= 'SELECT ... WHERE Table.nuentrada = :Param';
        Params.CreateParam(ftLargeint, 'Param', ptInput);
        ParamByName('Param').AsLargeInt:= Num;
        Open();
        DataSource.DataSet:= FDQuery;
      end;
  finally
    //FD.Free;
  end;
end;

♦ If you are using TADOQuery :

Const
  Num: Largeint = 350000701778;
Var
  ADOQuery: TADOQuery;
begin
  ADOQuery := TADOQuery.Create(Nil);
  try
    with ADOQuery do
      begin
        Connection:= ADOConnection1;
        SQL.Text:= 'SELECT ... WHERE Table.nuentrada = :Param';
        Parameters.ParamByName('Param').Value:= Num;
        Open;
        DataSource.DataSet:= ADOQuery;
      end;
  finally
    //ADOQuery.Free;
  end;
end;

[1] online documents .

[2] SQL Injection .     

03.12.2018 / 23:10
0

Good afternoon everyone.

The problem was solved by putting a MyQuery.next at the end of the while as below.

MyQuery.Close;

with MyQuery.SQL do
begin
     clear;
     add(' select TOP 8 nuentrada, dstpleitu, vlleitura, dshint, dtleitura, hhleitura ');
     add(' from ssdentsinaisvitais inner join ssttplei on ssdentsinaisvitais.cdleitura = ssttplei.cdleitura ');
     add(' where nuentrada = ' + lblnuentrada.Caption);
     add(' order by dtleitura desc, hhleitura desc ');
 end;

 MyQuery.Open;

 while MyQuery.Eof do
 begin
      mmdsexamefisico.Text := mmdsexamefisico.Text + MyQuery.DataSource.DataSet.FieldByName('dstpleitu').AsString + ': ';
      mmdsexamefisico.Text := mmdsexamefisico.Text + MyQuery.DataSource.DataSet.FieldByName('vlleitura').AsString + #13#10;
      MyQuery.next;
 end; 
    
04.12.2018 / 19:50