False/True is not a valid integer value for field blabla

Ok, so you designed a small mysql table with a boolean field which got translated to tinyint(1). You then wrote your query in delphi and populated your querys fields and the field got mapped to a TSmallIntField.
And then, on top of everything, you placed a db-aware checkbox on the form to set that field and now you are getting the error
“False/True is not a valid integer value for field blabla”.

What to do? Well, since delphi won’t map your field to a TBooleanField as it should (since the field is actually an integer), you must do it yourself. So, close the project, open your favorite text editor and modify the dfm and pas files so that those fields are now of type TBooleanField.

Then, right before the form declaration add the following:

TBooleanField=class(DB.TBooleanField)
public
constructor Create(AOwner: TComponent); override;
end;

and in the implementation section

{ TBooleanField }

constructor TBooleanField.Create(AOwner: TComponent);
begin
inherited;
SetDataType(ftSmallint);
end;

If you need this in more then one form, then create a unit, like MySqlBooleanField.pas and then add it to the uses clause AFTER the DB unit.

That’s it, you’re all set.

Related posts

Tags: ,

One Response to “False/True is not a valid integer value for field blabla”

  1. ciuly says:

    I forgot to mention the obvious:
    the tdbcheckbox has a valueChecked and ValueUnchecked fields. you set those to 1 and 0 respectivly and you should be fine. However, I do preffer having it as a TBooleanField as when you use it you can simply do

    if fieldname.value then // value being of type boolean so you have strong typing

Leave a Reply

This blog is kept spam free by WP-SpamFree.