Concatenate Sqlquery loop result

Asked

Viewed 330 times

2

I have a loop that scans a table, I need each row a variable type string be incremented with the respective records.

Use FDQuery and FDConnection.

Follow an idea of what I need

var linhas : string;

while not eof do
begin
linhas := linhas + FieldByName('nome').AsString + ', ';
end;

Result I need:

Mary, Mark, Ezekiel, ...

  • Can you make this concatenation using a list or vector? And from a function that returns random names?

  • @Jeffersonquesado I have no idea how to make a list or a vector. Using a Dbgrid it lists right. I need the "NAME" field of the table only. In PHP it was easier :(

  • You have several moving pieces here: string concatenation, talking to the bank, determining the end of the loop (maybe something else I can’t see). In these situations, try to isolate your problem. I am trying to suggest that you test in a more controlled environment. Try to go to the moving part that is not fitting. You’ve assumed you don’t know vectors in programming, so I strongly suggest studying more basic subjects before venturing into database information retrieval.

  • @Jeffersonquesado as I mentioned the result of the consultation I made the impasse is in the increment of the variable to each line that the loop travels. For the sake of understanding, the only thing I’m not succeeding at is concatenation. Regarding not knowing vectors only in Delphi I am not familiar. thank you

  • Make this clear in the text of the question then ;-)

  • @Jeffersonquesado thanks for trying to help. I believe the focus of the problem is well explained.

Show 1 more comment

1 answer

4


var  
  linhas: string;
begin
  FDQuery1.First;
  while not FDQuery1.Eof do
  begin
    linhas := linhas + FDQuery1.FieldByName('nome').AsString + ', ';

    FDQuery1.Next;
  end;
end;

As far as I understand your question, the above code will meet your need to concatenate the Query result into a string. You can also do this in a Stringlist, follow the code:

var
  linhas: TStringList;
begin
  linhas := TStringList.Create;
  FDQuery1.First;
  while not FDQuery1.Eof do
  begin
    linhas.Add(FDQuery1.FieldByName('nome').AsString);

    FDQuery1.Next;
  end;
end;
  • Ezekiel, taking into account your need, the first example of code I posted is what you need. Managed to apply it?

  • Hello Andrey, It worked round, I ended up using the first example because it already met the need. Big hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.