Recent

Author Topic: [Solved] [SQLite] Print a Query into a Tmemo (or export to Libroffice)  (Read 3138 times)

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
Hello

I have a query:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var temp: string;
begin
temp:= 'SELECT afatablefrim.Land, afatablefrim.Post, afatablefrim.Nummer, ';
temp:= temp + 'afatablefrim.Type, afatablefrim.Postfrisk, objekt.Samling, objekt.Kartotek, objekt.AlbumNavn, ';
temp:= temp + 'objekt.AlbumNummer, objekt.Side, objekt.Linie, objekt.Placering, ';
temp:= temp + 'objekt.Antal FROM afatablefrim INNER JOIN objekt ON ';
temp:= temp + 'afatablefrim.Nogle =  objekt.AFANogle ';
temp:= temp + 'GROUP BY afatablefrim.Land, afatablefrim.Post, ';
temp:= temp + 'afatablefrim.Nummer, afatablefrim.Type, afatablefrim.Postfrisk;';
If SQLQuery1.Active = true then SQLQuery1.Active:= false;
SQLQuery1.SQL.Clear;
SQLQuery1.SQL.Add(temp);
SQLQuery1.ExecSQL;
If SQLQuery1.Active = false then SQLQuery1.Active:= true;
temp:= '';
While Not(SQLQuery1.Eof) do
      begin
      temp:= temp + SQLQuery1.Fields[0].asstring;
      SQLQuery1.Next;
      end;
Showmessage (temp);   
Which is obviously working.
I would like to print it on a TMemo or something else, that I can export to Libroffice afterwards.
I would like it to be printed (almost) like this:
Code: [Select]
Land
  Post
    Nummer or Nummer Type
      Postfrisk
        Samling   Kartotek   AlbumNavn   AlbumNummer   Side   Linie   Placering
The 4 firste lines are from the master table.
The last line is coming from the slave.

Can you please tell me, how I can solve this with some links or explanations
In the meantime, I keep digging!  :)

Best Regards
Jean-Christophe
« Last Edit: September 01, 2014, 08:11:35 pm by jeanchristophe »

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: [SQLite] Print a Query into a Tmemo (or export to Libroffice)
« Reply #1 on: September 01, 2014, 10:02:41 am »
use function format() and memo1.lines.add().
But better use TStrings first and after all your data is found copy TStrings to your memo.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
Re: [SQLite] Print a Query into a Tmemo (or export to Libroffice)
« Reply #2 on: September 01, 2014, 10:10:36 am »
Thank for your answer mangakissa

My first problem is that all the rows are populated, even if I used "GROUP BY"
When I access the first: SQLQuery1.Fields[0].asstring;
I am expecting that the second one, to be empty, unless there is a new value, but not. The field (and the others) are always populated, even if there is no change!
I try to not add the "GROUP BY" fields in my "SELECT", but they are not showned at all then!

Best Regards
Jean-Christophe

LacaK

  • Hero Member
  • *****
  • Posts: 691
Re: [SQLite] Print a Query into a Tmemo (or export to Libroffice)
« Reply #3 on: September 01, 2014, 02:00:21 pm »
My first problem is that all the rows are populated, even if I used "GROUP BY"
It is expected behavior

I am expecting that the second one, to be empty, unless there is a new value, but not.
It is wrong expectation  :)

You must use local variable store in them group1:=SQLQuery1.Fields[0].asstring and in loop check
  if group1 <> SQLQuery1.Fields[0].asstring then begin
    group1:=SQLQuery1.Fields[0].asstring;
    // print group header: group1
  end;

jeanchristophe

  • Jr. Member
  • **
  • Posts: 88
Re: [SQLite] Print a Query into a Tmemo (or export to Libroffice)
« Reply #4 on: September 01, 2014, 06:18:34 pm »
OK, thank you LacaK!
I try that and get back.

Best Regards
Jean-Christophe

 

TinyPortal © 2005-2018