March 10th, 2012, 05:10 AM
Creating a comma seperated text file from data
I want to create a text file from a DATASET (grid or table). I want to add my own seperator and save it to a location of my own choice. Lets say i have a dbgrid with 30000 records and i need to loop through it this would take ages to export? Am a right? Adding headings should not be a problem but is there a way of knowing the amount of fields in a datatset?So that i can control it dynamically. And can i read the HEADINGS of a datatset or query?
Thanks in advanced?
March 11th, 2012, 06:06 PM
This is a huge question that begs its own set of questions
I know because I have done this (partially?) myself.
1. Have you decided which field types you are going to support and
what to do about field types you will not support?
e.g. BLOB fields, other fields that do not override AsString
2. Work with the TDataset and make sure it is not connected to a DB aware grid
or any other GUI components as that will slow down processing dramatically.
3. Take a good look at the TDataset, TFields and TField objects, you will find that they give you access
to all the information you need. But remember item 1. above.
TDataset.FieldCount may NOT be the number of fields you can or want to represent.
4. From 1,3 above - what to do about BLOB fields etc.
Put in default text like <BLOB> (in every row?)?
Put in description <Picture> or <rich text>?
5. How will you format numeric fields?
OK. I could go on for a while; but this is, I hope, enough to get you thinking.
If it leads to more targeted questions then please post a follow up.
March 14th, 2012, 04:11 AM
Thanks for the Reply. All valid questions. I will have to support blobs. I was thinking of just just using the formats that is in the DATASET. If it is in the table then i know all information is correct. No extra formatting needed. Only thing i have to check obviously is all string or blob fields that there is no commas (,) in the words as it will bugger up my text file if i open it in excel.If you know what i mean. There wil only be text(string), blob, decimal and numbers(integer) and date fields. Nothing fancy.No images or such.
I have a nice export utility that does this for me. Only problem is if i open it in excel each line gets put in the first column. I have emailed the guy that created the component but he said he does not see anything wrong. Somehow i thinkk you shoudl be able to put some kind of character after each line to tell it its a new line? Am i right?
Maybe i will start solong and see what i get? Thanks for your help so far!
March 14th, 2012, 02:18 PM
If you are going to support BLOB fields you are, presumably, going to have to consider line lengths if you want it to be viewable.
Also you will need to deal with coding/decoding the BLOB data into Base64, in addition to the issues you mention.
Before you iterate the records, call the Dataset's DisableControls method. And when you finish iterating, call that Dataset's EnableControls. This way when you are iterating, any linked data aware controls will not be notified about record changing so they don't have to update their "look". This will greatly speed up your iteration.
Originally Posted by icuras
Sure, very easy. For number of fields of a dataset, just check FieldCount property of the Dataset after it is opened. For field names, iterate Fields property of the Dataset.
There is no heading(s) of dataset. The closest thing to it is maybe the fields names. If you are using persistent field. You might want to use/check DisplayLabel property of the persistent field. However DisplayLabel is set manually in design time. It does not get its value from the underlying database.
For CSV, this char(s) is CR-LF (Carriage Return, #13, and Line Feed, #10). However CR only is usually fine too (especially in unix environment.