Results 1 to 5 of 5

Thread: save the result of a query on a text file

  1. #1

    save the result of a query on a text file

    hi people,

    I'm noob on that ops:
    i have to save the result of a query in a text file, which is the easiest way for do that?

    I don't want to install new components i think delphi standard is enought (i'm using delphi 5)

    ty for help.
    Will: "Before you learn how to cook a fish you must first learn how to catch a fish." coolest

  2. #2

    save the result of a query on a text file

    Can you provide a bit more information on what exactly you are wanting to do? Saving the results of a query to a text file is a bit vague. Are you looking to do something like in SQL Server Management Console where you can copy the results and paste them into a text file, and if so what Database are you working out of? Are you using a generic provider like ADO and want a generic storage hander? Do you want cross platform or just windows? I could go on, but I think you get the idea.

  3. #3

    save the result of a query on a text file

    I connect through ODBC to a mysql server;
    the server is running winxp and the client too, i'm using Tquery component for execute query.

    i just wanna save the result of a select query on a file with column separetor, ex:

    name|surname|age|etc..
    Will: "Before you learn how to cook a fish you must first learn how to catch a fish." coolest

  4. #4
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    save the result of a query on a text file

    This code snippet assumes that the query is already open. It puts " around strings and uses a comma for the separator, and dumps each record as a new line in a TMemo component.

    [pascal]
    var
    fieldLoop : integer;
    temp : string;
    begin
    while (not qry.eof) do
    begin
    temp:='';

    for fieldLoop:=0 to qry.fieldCount-1 do
    begin
    if (temp<>'') then
    temp:=temp+',';

    if (qry.Fields[fieldLoop] is TStringField) then
    temp:=temp+'"'+qry.fields[fieldLoop].asString+'"'
    else
    temp:=temp+qry.fields[fieldLoop].asString;
    end;

    memo1.lines.add(temp);

    qry.next;
    end;

    end;
    [/pascal]

    Its probably not perfect in that there are possibly other field types that should be wrapper in quotes, but its a start.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  5. #5

    save the result of a query on a text file

    ty i made something like this and work good:

    Code:
    procedure TForm1.SaveReport&#40;const id_report&#58; string&#41;;   
    var
        sSql,s, sCampi&#58; String;
        F1&#58; Textfile;
    begin
    
    
    
    
        try
    // - STEP 010
            sSql &#58;='SELECT ......something ';
    
    
            setQry&#40;sSql,qry_dyn&#41;;
            qry_dyn.Active &#58;= True;
            qry_dyn.First;
    
            Savedialog1.DefaultExt&#58;='*.csv';
            SaveDialog1.Filter &#58;= 'Comma separeted files &#40;*.csv&#41;|*.CSV|Text files &#40;*.txt&#41;|*.TXT';
    
            if SaveDialog1.Execute then
                 AssignFile&#40;F1, SaveDialog1.Filename&#41;
            else Exit;
    
            Rewrite&#40;F1&#41;;  
    
            
            while not qry_dyn.Eof do
                begin
                  writeln&#40;F1,
                  qry_dyn.FieldByName&#40;'a'&#41;.AsString + '|' +
                  qry_dyn.FieldByName&#40;'b'&#41;.AsString + '|' +
                  //.....
                  qry_dyn.FieldByName&#40;'z'&#41;.AsString&#41;;
    
                  qry_dyn.Next;
                end; //while 
            if &#40;Debug_enable > 1&#41;  then caricaLogProcedure&#40;'TForm1.SaveReport;','010','OK',''&#41;;
        except
            try
                    Closefile&#40;F1&#41;;
            except
                    //errore nella chiususa file
                    ShowMessage&#40;'Errore duranta la scrittura del tipo1, impossibile chiudere il file'&#41;;
            end;
            ShowMessage&#40;'Bugggone!'&#41;;
            if &#40;Debug_enable > 0&#41;  then caricaLogProcedure&#40;'TForm1.SaveReport;','010','KO',sSql&#41;;
        end;
    
        Closefile&#40;F1&#41;;
    
    
    end;
    Will: &quot;Before you learn how to cook a fish you must first learn how to catch a fish.&quot; coolest

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •