Hi
I have a procedure in my program that saves the data that is in DBGrid cells for 4 columns to a CSV file. The datasource for the DBGrid is SQLite database.
The save to CSV works well. But, it is not as fast as I would like. For example, a friend recently used it to save the results of about 500K rows of data. The process took a couple of hours. By contrast, when he used SQLite Explorer and did the export directly from that using the SQLite database from my program, it took about 4 seconds.
So I am wanting to try and work out how I might make this process of writing the data faster? And insights gratefully received.
Here is the existing procedure :
procedure TfrmSQLiteDBases.SaveC2FDBToCSV(DBGrid : TDBGrid; Filename : string);
var
linetowrite : ansistring;
n : integer;
CSVFileToWrite : TFilestreamUTF8;
begin
Mainform.StatusBar2.SimpleText := 'Writing results to file...please wait';
Application.ProcessMessages;
linetowrite := '';
n := 0;
try
CSVFileToWrite := TFileStreamUTF8.Create(Filename, fmCreate);
DBGrid.DataSource.DataSet.First;
while not DBGrid.DataSource.DataSet.EOF do
begin
// Include all columns
linetowrite := (DBGrid.DataSource.DataSet.Fields[1].Text) + ',' +
(DBGrid.DataSource.DataSet.Fields[2].Text) + ',' +
(DBGrid.DataSource.DataSet.Fields[3].Text) + ',' +
(DBGrid.DataSource.DataSet.Fields[4].Text) + #13#10;
n := 0;
n := Length(linetowrite);
try
CSVFileToWrite.Write(linetowrite[1], n);
finally
DBGrid.DataSource.DataSet.Next;
end;
end;
finally
CSVFileToWrite.Free;
end;
Mainform.StatusBar2.SimpleText := 'DONE';
ShowMessage('Grid data now in ' + Filename);
end;