0
votes

I am working with the Delphi FireDac technology in connection with a PostgreSQL database. I would like to set a floating value as DefaultExpression for a Field at Runtime. The next lines works fine:

FDMemTable1.FieldByName( 'some_fieldname').FieldKind  := fkData;
FDMemTable1.FieldByName( 'some_fieldname').DefaultExpression := '5.5';

But when I try to factor this out with the next code, I get an error: Exception class EFDException with message '[FireDAC][Stan][Eval]-104. Type mismatch in expression'.

procedure SetDefaultFieldValue (
    var aFDMemTable : TFDMemTable;
    aFieldName  : string;
    aFieldValue : string;
    aFieldKind  : TFieldKind );
  begin
    aFDMemTable.FieldByName( aFieldName ).FieldKind  := aFieldKind;
    aFDMemTable.FieldByName( aFieldName ).DefaultExpression :=  QuotedStr( aFieldValue );
  end;

What am I doing wrong?


Thanks all for the suggestions. The problem seems to be - as @Graig Young pointed out- the different kind of columns (Numeric or String etc). I needed the QuotedStr for the String-Fields. Otherwise I get an error:

[aFieldValue] Column or Function is not found. Add FireDAC.Stan.ExprFuncs to the uses clause (which I have)

So @Victoria, leaving out QuotedStr works indeed for the Numeric-Field, but factoring like the next lines doesn't work for String-Fields.

procedure SetDefaultFieldValue (
    var aFDMemTable : TFDMemTable;
    aFieldName  : string;
    aFieldValue : string;
    aFieldKind  : TFieldKind );
  begin
    aFDMemTable.FieldByName( aFieldName ).FieldKind  := aFieldKind;
    aFDMemTable.FieldByName( aFieldName ).DefaultExpression :=  aFieldValue;
  end;

DefaultExpression expects a string-value. Trying to refine my question: Is it possible to factor-out DefaultExpression for "any" FieldType? if so, how?

1
This is not a minimal reproducible example The "type mismatch" error message suggests you might be trying to set a string default for a numeric column. I suggest you edit and fix your question. - Disillusioned
Please read the link on minimal reproducible examples. The sooner you learn to do this, the much easier time you'll have asking coding questions (and not just here). - Disillusioned

1 Answers

0
votes

Sorry for any unclearness, but the next code avoids the mentioned errors.

procedure SetDefaultFieldValue(
    var aFDMemTable : TFDMemTable;
    aFieldName      : string;
    aFieldValue     : string;
    aFieldType      : TFieldType);
  begin
    aFDMemTable.FieldByName( aFieldName ).FieldKind  := fkData;

    case aFieldType of
      ftString  : aFDMemTable.FieldByName( aFieldName ).DefaultExpression :=  QuotedStr( aFieldValue ) ;
      ftSingle  : aFDMemTable.FieldByName( aFieldName ).DefaultExpression :=  aFieldValue ;
      ftInteger : aFDMemTable.FieldByName( aFieldName ).DefaultExpression :=  aFieldValue ;
    end;

  end;

As @Graig Young mentioned the way of handling String-Fields should be different form Numeric-Fields.

Now the next code works.

SetDefaultFieldValue( aAliasTable.FDMemTable1, 'some_fieldname', 'a_string_value' , ftString );
SetDefaultFieldValue( aAliasTable.FDMemTable1, 'another_fieldname', '5.5' , ftSingle );

Thanks to all!