[CDBI] How find_or_create method handle float data type?

Oliver Jeeves oj at defuturo.co.uk
Tue Oct 3 09:06:28 BST 2006


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://lists.digitalcraftsmen.net/pipermail/classdbi/attachments/20061003/a14c70ea/signature.pgp


More information about the ClassDBI mailing list