Re: [CDBI] How find_or_create method handle float data type?
Oliver Jeeves <[email protected]>
| Newsgroups | gmane.comp.lang.perl.modules.class-dbi |
|---|---|
| Message-ID | <[email protected]> |
David Zhao wrote:
> Hi there,
>
> I'm new to Class::DBI, and have a question. How can I use find_or_create method
> if I have columns with float data type?
> my $id = DB::MySQLDb::MyTable->find_or_create({ float_column => 2.01})
> doesn't work, it returns a new id every time I run it. In mysql sql statement, I
> could do "select * from mytable where float_column - 2.01 < 0.001", how about
> using Class-DBI?
> Thanks!
>
> David
Float would appear to be a poor choice for a column that you want to be
unique, for the simple reason that you're always going to run into this
problem - the numbers that you can represent accuratly with fixed
precision decimal are different to the numbers you can represent
accuratly with fixed precision binary, so certain numbers you'll never
be able to compare and get equality. In the same way that 1/3 can't be
represented in decimal, 1/10 can't be represented in binary.
You probably want to use Class::DBI::AbstractSearch and do something
similar to what you have done before - looking for records within a
certain range of the value you want, and creating one if none were found.
Alternativly, if your floating point numbers will always have a fixed
number of decimal places, like currancy, for example, you might want to
consider representing this as an integer, and converting it to decimal
in your application.
-Oli
_______________________________________________
ClassDBI mailing list
ClassDBI-Ra3b/[email protected]
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi
signature.asc
(application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iD8DBQFFIhoMSbWXajvTQ+URAiR3AJwIz1Fm5ZKn2i5+GfCh3IVndHCzhwCeNOOU abaTolYwANO6xTsxD8+M3bM= =8Dqj -----END PGP SIGNATURE-----