Link to home
Start Free TrialLog in
Avatar of gi7mhl
gi7mhl

asked on

CSV files

is it possible to use sql to delete records from a csv database. if not what alternative can i use to modify my csv database (alter field values, delete records).
ASKER CERTIFIED SOLUTION
Avatar of DragonSlayer
DragonSlayer
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VGR
VGR

Or use Delphi to read the comma-delimited file, put the records in a TGrid of some kind, add/delete/modify records, and write the CSV file back...

this is the Delphi section, after all :D
You can create a custom dataset using whatever underlying file type you like. You can either use the BDE or ADO to connect to it.
FrankTheRat
Hello!

A CSV file has comma separated values.
To get the column values of each row, you could use the commatext property of a TStrings.

example code

MyCSVProc(FlNm: String);
Var
  MyCSVFL, MyCSVRow: TStringList;
  FilRecCtr, FilColCtr: Integer;

Begin
  Try
  begin
    MyCSVFl := TStringList.Create;
    MyCSVFl.LoadFromFile(FlNm);//Load CSV file
    MyCSVRow := TSTringList.Create;
    //This if for the rows
    For FilRecCtr := 0 to MyCSVFl.Count -1 Do
    Begin
      //read record
      MyCSVRow.CommaText := MyCSVFL[FilRecCtr];

      (* if you are using a StringGrid *)
      //stringGrid1.Rows[FilRecCtr].CommaText := MyCSVFL[FilRecCtr];

      //This if for the columns
      FOr FilColCtr := 0 to MyCSVRow.Count -1 Do
      Begin
        //Code your modification logic here.
        MyCSVRow[FilColCtr] := MyCSVRow[FilColCtr];

      End;
      //write modified record
      MyCSVFL[FilRecCtr] := MyCSVRow.CommaText;

    End;


    MyCSVFl.SaveToFile(FlNm);//Save File/Database


  end
  except on e: exception do
    ShowMessage(e.Message);

  end;


End;

...Snehanshu

"When you are courting a nice girl an hour seems like a second. When you sit on a red-hot cinder a second seems like an hour. That's relativity."
It seems that the ttASCII table type of Delphi's TTable does not support CSV as documented.

I found a freeware component that lets you use a csv file as a table (hopefully supporting queries et al).

You could Download and install:

http://www.leif-bruder.de/download/LbCsv.zip

I could view the csv file in a DBGrid using this component and a dataset.

...Snehanshu
Why don't you extend the Demos/db/textdata example to support CSV files instead of working on a line-per-line basis ?

I really doub't you'll find a CSV TDataSet descendant with an SQL parser and optimizer engine that will support queries.

A more simple approach would be for you to use a some sort of TMemoryTable and load the CSV file to it.  That will enable all dbaware controls and all you'll have to do is manually load and save the contents of the memory stream to your csv file (which should be quite simple).

An example of an open-source / freeware TMemoryTable can be found in RXLib.