[fpc-pascal] Trouble setting/getting ftVarBytes field data in TBufDataset
Reinier Olislagers
reinierolislagers at gmail.com
Tue Aug 2 07:39:49 CEST 2011
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);
writeln('3. Or like this?');
TempPChar:=PChar(TestString);
TestDataSet.FieldByName('ftVarBytes').SetData(TempPChar);
writeln('ftVarbytes.AsString: ' +
TestDataset.FieldByName('ftVarBytes').AsString);
TestDataset.Post;
writeln('4. Get data using this:');
writeln('ftVarbytes.AsString: ' +
TestDataset.FieldByName('ftVarBytes').AsString);
writeln('5. Or get data using this:');
TempPchar:='';
if TestDataset.FieldByName('ftVarBytes').GetData(TempPChar) then
begin
writeln('We filled TempPChar with: ' + TempPChar);
end
else
begin
writeln('The Getdata function didn''t work.');
end;
TestDataset.Close;
TestDataset.Free;
end.
Output:
1. How do I fill an ftVarBytes field?
2. Like this?
ftVarbytes field contains: How
3. Or like this?
ftVarbytes field contains: How
4. Get data using this:
ftVarbytes field contains: How
5. Or get data using this:
We filled TempPChar with:
!The Getdata function didn't work.
TObject♦
<snip more data from memory>
More information about the fpc-pascal
mailing list