[fpc-pascal] Bump: Trouble setting/getting ftVarBytes field data inTBufDataset
Ludo Brands
ludo.brands at free.fr
Wed Aug 3 12:32:48 CEST 2011
> On 2-8-2011 7:39, Reinier Olislagers wrote:
> > I've got trouble finding out how to fill and retrieve data for a
> > ftVarBytes field in a TBufDataset.
> >
> > .AsString doesn't seem to work well - if I fill it with the
> string How
> > do I fill this field? I get back
> > How
> >
> > I tried SetData, which takes a buffer as data, so I tried filling a
> > PChar with the string data. Retrieving it leads to a big dump of
> > memory, so I wonder how you define the end of the buffer in the
> > SetData call.
> >
> > Hope somebody can point me to some documentation or tell me
> the proper
> > way to do this. I've looked at Delphi docs, but that dealt
> mostly with
> > general BLOB fields, don't know if that includes varBytes.
> >
> > Below some test code and output:
> > program varbytesproject;
> >
> > {$mode objfpc}{$H+}
> > {$APPTYPE CONSOLE}
> >
> > uses
> > {$IFDEF UNIX}{$IFDEF UseCThreads}
> > cthreads,
> > {$ENDIF}{$ENDIF}
> > Classes, SysUtils,
> > { you can add units after this }
> > DB, BufDataSet;
> >
> > var
> > TestDataset: TBufDataset;
> > FieldDef: TFieldDef;
> > TestString: String;
> > TempPChar: PChar;
> > begin
> > TestDataset := TBufDataset.Create(nil);
> > FieldDef := TestDataset.FieldDefs.AddFieldDef;
> > FieldDef.Name := 'ftVarBytes';
> > FieldDef.DataType := ftVarBytes;
> > FieldDef.Size := 2;
> > TestDataset.CreateDataSet;
> > TestDataset.Open;
> > TestDataset.Append;
> >
> > writeln('1. How do I fill an ftVarBytes field?');
> > Teststring:='How do I fill this field?';
> > writeln('2. Like this?');
> > TestDataset.FieldByName('ftVarBytes').Asstring := Teststring;
> > writeln('ftVarbytes.AsString: ' +
> > TestDataset.FieldByName('ftVarBytes').AsString);
This is the way to do it. However:
- FieldDef.Size := 2; You only store 2 characters.
- TCustomBufDataset.GetFieldSize doesn't recognise TBinaryField descendants
and fixes their length arbitrary as 10 bytes... Setting fieldsize to more
than 10 bytes won't store more than 10 chars. TMemDataset handles this more
elegantly by raising a SErrFieldTypeNotSupported.
So the answer is that TBufDataset doesn' support TBinaryField descendants
correctly.
For test purposes, limit field size to 10 bytes ;)
Ludo
More information about the fpc-pascal
mailing list