Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (5)


THvHQuery for exporting query to CSV file.Format this article printer-friendly!Bookmark function is only available for registered users!
Export a query to a Comma Seperated CSV File.
Product:
Delphi 5.x (or higher)
Category:
DB-General
Skill Level:
Scoring:
Last Update:
09/09/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet CSV Query Export Component
Times Scored:
1
Visits:
3318
Uploader: Henk van Hoek
Company:
Reference: N/A
 
Question/Problem/Abstract:
Sometimes you need a quick solution for exporting a TQuery result to a CSV file.
Answer:



Register the next component to your component pallet.
How to use:
You can use the property editor to fill in the filename to write too.
You can specify if you want to write the Fieldnames (CSVHeaders).
You can specify if you want to append to the file.

You have to implement your own error handling strategy for the opening of the file. The propery for returning the FileError is there already.


unit HvHQuery;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, DBTables;

type
  THvHQuery = class(TQuery)
  private
    { Private declarations }
    FFileName: string;
    FErrorCodeWriteCSV: integer;
    FCSVHeaders: boolean;
    FErrorCode: integer;
    FAppendToCSVFile: boolean;
    F:TextFile;
    procedure SetCSVHeader(Value: boolean);
    procedure SetErrorCode(Value: integer);
    procedure WriteCSVHeaders;
    procedure WriteCSVValues;
  protected
    { Protected declarations }
  public
    procedure WriteCSVFile;
    { Public declarations }
  published
    { Published declarations }
    property FileName: string read FFileName write FFileName;
    property ErrorCode: integer  read FErrorCodeWriteCSV write SetErrorCode;
    property CSVHeaders: boolean read FCSVHeaders write SetCSVHeader default False;
    property AppendToCSVFile: boolean read FAppendToCSVFile write FAppendToCSVFile default False;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('HvH', [THvHQuery]);
end;

procedure THvHQuery.SetCSVHeader(value: boolean);
begin
  FCSVHeaders := Value;
end;

procedure THvHQuery.SetErrorCode(Value: integer);
begin
  FErrorCode := Value;
end;


procedure THvHQuery.WriteCSVHeaders;
var
  i: integer;
begin
  if FCSVHeaders then begin
    i := 0;
    While i < Fields.Count do begin

      Write (F, format('"%s"', [Fields[i].FieldName]));
      if i < Fields.Count - 1 then
        Write (F, ';')
      else
        WriteLn(F);
      i := i + 1;
    end;
  end;
end;

procedure THvHQuery.WriteCSVValues;
var
  i: integer;
begin
    i := 0;
    While i < Fields.Count do begin

      Write (F, '"' + Fields[i].Text + '"');
      if i < Fields.Count - 1 then
        Write (F, ';')
      else
         WriteLn(F);

      i := i + 1;
    end;

end;

procedure THvHQuery.WriteCSVFile;
var
i: integer;
begin
  AssignFile(F, FFileName);
  if FAppendToCSVFile then
    System.Append(F)
  else
    ReWrite(F);

  First;
  WriteCSVHeaders;

  while not EOF do begin
    WriteCSVValues;
    Next;
  end;

  CloseFile(F);
end;


end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
do not derive from TQuery
    Andreas Schmidt (Sep 11 2003 7:07PM)

You should *not* derive your CSV-Class from TQuery,
because this is very unflexible.

Instead derive your class from TComponent and
add a property to your class:
property Dataset:TDataSet read FDataset write FDataset.

This gives you the possibility to attach your CSV object to
any Dataset (TTable, TQuery, TADOTable, TADOQuery, TIBQuery,...).


Respond

RE: do not derive from TQuery
Henk van Hoek (Sep 16 2003 10:01AM)

You are correct.I will post an update soon.
Respond

RE: RE: do not derive from TQuery
Luis Jourdan (Jun 30 2007 3:45AM)

Hello Did you upgrade this code, I tried to use it with Interbase but it does not work Kind regards
Respond

RE: RE: RE: do not derive from TQuery
Henk van Hoek (Jun 30 2007 12:37PM)

I don't see why it should not work with or without the update with Interbase. Can you tell what is wrong.

By the way. The compononent is NOT threadsafe.
Respond

RE: RE: RE: RE: do not derive from TQuery
Luis Jourdan (Jul 2 2007 5:43PM)

I think because the query component form dbase has differente properties than te interbase one.
Kins regards
Alejandro Jourdan
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
A. B. Talal
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)