Forum > Databases

ftBytes in TBufDataset

(1/1)

ojz0r:
I'm trying to use ftBytes as fieldtype in a BufDataset but it doesn't behave the way i expect it to do.
When i set the Size attribute of the field to 1 i expect it to allocate 1 byte, so max value 255 (or $FF). However it seems that "Size" corresponds to number of digits, if i try to enter 123 it will only keep 1, if i set size to 2 it will keep 12, and if i set size to 3 it will keep 123. However that means that it will accept values up to 999.

Is there something im doing wrong or is this a bug?
I've tried to search for "ftBytes" and "ftByte" but i can't find anything relevant.

Zvoni:
https://www.freepascal.org/docs-html/fcl/db/tfieldtype.html

The only thing coming close would be ftSmallInt, but that's signed
ftBytes is an Array of Bytes

--- Quote ---*snip*
ftTime,  Time value
ftDateTime, Date/Time (timestamp) value
ftBytes,  Array of bytes value, fixed size (unytped)
ftVarBytes, Array of bytes value, variable size (untyped)
ftAutoInc, Auto-increment integer value (4 bytes)
*snip*
--- End quote ---

That said: Set the "size" to 0

ojz0r:
Okay so no real byte type then.
Smallint seems variable depending on CPU architecture. For 64-bit system a Smallint is 16-bit signed integer, so +/-32764.

Richard Marriott:
As already mentioned ftBytes represents an array of byte ( defined as TBytes), I manged to get it to work with this code:

      FieldDefs.Add('XBYTES',ftBytes,1,false);  //1I byte in length

Then to add/retreive data after CreateTable or CreateDataset do this

     Setlength(tb,1);                                              // tb is type TBytes 
     tb[0] := b;                                                       // where b is a number of type byte (0..255)
     FieldsByName('XBYTES').AsBytes := tb;     // Save the value to the buffer
   
     tb2 := FieldsByName('XBYTES').AsBytes;  // Retrieve the value into tb2 which is type TBytes     
     b2 := tb2[0];                                                 // Same as original input
   
If you do this (as TDBGrid would do)
    s := FieldsByName('XBYTES'].AsString;     // s is chr(b2)  ie the character equivalent

Hope that helps. Else as already suggested use ftSmallInt (2 bytes) with external code to do range checking. I think that would be simpler. ftBytes is really best for binary arrays where you need to know exactly what is contained in each byte and process each byte separately.
 

Richard Marriott:
Just had a better idea that works and is simpler. Try this:

      FieldDefs.Add('XBYTES',ftBytes,1,false);  //1 byte in length

Then to add/retrieve data after CreateTable or CreateDataset do this

     b1 := 123;                                                        // b1 is type byte. assign any valid number 
     FieldsByName('XBYTES').SetData(@b1);     // Save the value to the buffer
   
    FieldsByName('XBYTES').GetData(@b2);  // Retrieve the value into b2 of type byte  result is 123 
 

Navigation

[0] Message Index

Go to full version