Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 02-14-2017, 11:10 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 294
Default TFileStream.write and out of memory

i know i am doing something wrong but i just don't see it.
Code:
procedure TForm3.Button1Click(Sender: TObject);
var
  _afile  : TFileStream;
  _aline : string;
  _ftypes : string;
  _ctr,
  I: Integer;
begin
  _afile := TFileStream.Create('t:\dbf\myfile.csv', fmCreate or fmOpenWrite);
  _aline := string(fdquery1.Fields[0].FieldName);
  _ctr := 0;
  for I := 1 to fdquery1.FieldCount - 1 do
  begin
    _aline := _aline + ',' + string(fdquery1.Fields[I].FieldName);
  end;
  _aline := _aline + #13#10;
  _afile.Write(_aline[1], Length(_aline) * SizeOf(Char));
  while not(fdquery1.Eof) do
  begin
    _aline := trim(fdquery1.Fields[0].AsString);
    for I := 1 to fdquery1.FieldCount - 1 do
    begin
      if (fdquery1.Fields[I].DataType = ftString) or
        (fdquery1.Fields[I].DataType = ftWideString) or
        (fdquery1.Fields[I].DataType = ftmemo) or
        (fdquery1.Fields[I].DataType = ftWideMemo) then
          if (fdquery1.Fields[I].Value <> null) then
            _aline := _aline + ',' + string(fdquery1.Fields[I].Value)
          else
            _aline := _aline + ',';
      if (fdquery1.Fields[I].DataType = ftInteger) then
          _aline := _aline + ',' + IntToStr(fdquery1.Fields[I].Value);
      if (fdquery1.Fields[I].DataType = ftBCD) then
          _aline := _aline + ',' + FloatToStr(fdquery1.Fields[I].Value);
      if (fdquery1.Fields[I].DataType = ftDate) or
         (fdquery1.Fields[I].DataType = ftTimeStamp) then
          _aline := _aline + ',' + DateToStr(fdquery1.Fields[I].Value);
      if (fdquery1.Fields[I].DataType = ftBoolean) then
          _aline := _aline + ',' + BoolToStr(fdquery1.Fields[I].Value);
    end;
      _aline := _aline + #13#10;
    _afile.Write(_aline[1], Length(_aline) * SizeOf(Char));
//    inc(_ctr);
    if _ctr > 100 then
      Break;
    fdquery1.Next;
  end;
  _afile.Free;
end;
  #2  
Old 02-15-2017, 02:56 AM
lexd lexd is offline
Senior Member
 
Join Date: Feb 2015
Posts: 248
Default

_aline := string(fdquery1.Fields[0].FieldName); is an error
_aline is a filestream object pointer effectively (reference to its object methods and not recorded data in the object). Use the methods to access the data in and out of the object.

a) you need to put fdquery1.Fields[0].FieldName in a string
Get the length of the string
Read it into the filestream individually each line with a separator or line identity of some sort.
and make a loop to move all the data.

b) find the Memory block used by your object and copy the block if its possible (advanced)
  #3  
Old 02-15-2017, 03:09 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 294
Default Thanks for info but have another question...

the reason i added the string(....) part for the field name was due to the field names looking like this when i used a stringlist to write this info.
c[nul]i[nul]d[nul],c[nul]i[nul]d[nul]2[nul]
the table in question is a dbf table and i was not sure why it is in such a funky way but by adding the string(...) it removed all of the [nul]s
  #4  
Old 02-15-2017, 06:34 PM
lexd lexd is offline
Senior Member
 
Join Date: Feb 2015
Posts: 248
Default

it also lost your object so you cannot use it
  #5  
Old 02-16-2017, 05:46 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 294
Default discovered the OOM error was due to ODBC driver

apparently had to get a new ole db provider and use ADO

Will close issue thanks for all the info have rewritten code based upon another article i found:
Code:
procedure TForm3.Button2Click(Sender: TObject);
var
  fld : TField;
  _tbl : TStringlist;
  _afile  : TStreamWriter;
  I: Integer;
begin
  if not(ADOQuery1.Active) then
    ADOQuery1.Active := True;
  _afile := TStreamWriter.Create('t:\dbf\ar012.csv', false, TEncoding.UTF8);
  _afile.AutoFlush := True;
  _afile.NewLine := sLineBreak;
  _tbl := TStringlist.Create;
  _tbl.Clear;
  _tbl.QuoteChar := '"';
  _tbl.Delimiter := ';';
  ADOQuery1.GetFieldNames(_tbl);
  _afile.WriteLine(_tbl.DelimitedText);
  ADOQuery1.First;
  while not(ADOQuery1.Eof) do
  begin
    _tbl.Clear;
    for fld in ADOQuery1.Fields do
      _tbl.Add(fld.Text);
    _afile.WriteLine(_tbl.DelimitedText);
    ADOQuery1.Next;
  end;
  FreeAndNil(_tbl);
  _afile.Free;
end;
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 02:09 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.