[Lazarus] DBGrid displays Float as integer

Michael Van Canneyt michael at freepascal.org
Thu Feb 1 11:30:30 CET 2024



On Thu, 1 Feb 2024, John Landmesser via lazarus wrote:

>
> In my SQlite Database laufdaten.db column "km_gelaufen"  is of Type
> float. But SQlite does not know this type and if i understood that
> correct SQLite  creates this type as needed?!
>
> If i execute: "SELECT CAST(AVG(km_gelaufen) *AS REAL*) FROM laufdaten"
> the known problem of integer only shows up again.
>
> BUT:
>
> If i execute this "SELECT CAST(AVG(km_gelaufen)  AS REAL) FROM laufdaten
> " in SQliteStudio it works as it should.
>
> Is DBgrid or Zeos Access components  the culpit? I don't know.

For Object pascal, sqlite is something of a problem as it does not have real "types".

The following works:

sqlite> create table abc (id int);
sqlite> insert into abc values ('xyz');

Which is of course total nonsense. It should refuse the insert, but it does not.

The FPC database layer will do its best to detect the type of a result field, 
but there is no guarantee that it will be the correct type, or that the actual 
data will be of the correct type.
You will experience problems with data such as the above.

What you can do is to check

YourQuery.Fields[0].ClassName

if it has something different from TFloatField or TBCDField, then you know it has not correctly 
detected the type.

But if you value correctness and integrity of your data, don't use sqlite.
At best it should be used for config or textual data. Anything else is risky.

Michael.


More information about the lazarus mailing list