by nageswaragunupudi » Wed Nov 12, 2014 4:39 am
Not only MySql, behavior of VarChar fields is the same in all RDMSs.
In Sql DBMSs fieldtype CHAR(nwidth) behaves almost like our DBF fields.
But CHAR field is very rarely used and for a good reason.
Good practice is to use VARCHAR(nwidth) fields. Also it is good practice to store Trimmed values. We can not store strings larger than the DefinedSize. (This raises an error).
When we read we get the string of the same length as we have stored.
It is possible to store padded strings. We can store 'Hello '. In such a case when we read we get the value including the padded spaces when we stored. But this is not a good practice. It is good to store trimmed values.
We need padded values only for using in GETs. For this purpose we need to pad the values in our program. eg., cVal := PadR( oRs:Fields( "fieldname" ):Value, oRs:Fields( "fieldname" ):DefinedSize )
Agreed that this is combursome.
FWH comes in for some support here.
XBrowse:
When reading the field value is padded with spaces and displayed. When editing inline in browse, GET receives the padded value. While storing XBrowse stores Trimmed values.
This way XBrowse follows good practices in maintaining the SQL Table and at the same time gives the programmer the same experience as we handle DBF tables. ( If we give up the old habit of directly using bStrdata, etc )
TDataRow:
Same way like XBrowse the values are padded for editing and trimmed for storing.
TRecSet (new class)
Instead of using FW_OpenRecordSet(...), it is now recommended to use
oRs := TRecSet():New():Open( cSql, oCn )
Fields can be referred to as:
? oRs:FirstName
? oRs:Salary
oRs:Age := 40
TRecSet class, by default, displays character values Padded and stores trimmed values.
Example:
Assumomg field "CustName" definedsize is 30
oRs:CustName := "John"
? Len( oRs:Custname ) --> 30
oRs:Custname returns "John" padded to 30 chars.
All these 3 classes do this padding and trimming transparently. This reduces all the coding burdens.
Regards
G. N. Rao.
Hyderabad, India