in reply to Re: DBI: when 1 != '1'
in thread DBI: when 1 != '1'
I'll pass on the ability of Perl to distinguish between 1 and '1'. To my knowledge there's no way to separate themBut there is: for bitwise operators like "&" and "|", strings behave differently than numbers. If one is a number, both will be treated as a number. For example
will produce a different result for 1 and for "1".$\ = "\n"; for $a (1, "1") { print $a | " $a"; }
Coming back to DBI: It doesn't make this kind of distinction IIRC. Anything is a string.
Binding placeholders to a variable type can help. I don't know for sure for enumerated types in MyQSL, I can't test that (easily), but I do recall that DBI appears to make a guess on a field type based on the first value you feed it. If it looks like a number, DBI will treat the column as a number, which will produce an error if the field contains a non-numerical value for one of the next rows. I think even that if the first value is undef, DBI treats it as a string... And various MS databases do mind, even if MySQL doesn't.
This prevents that:
use DBI ':sql_types'; my $dbh = DBI->connect(...); my $sth = $dbh->prepare("SELECT * FROM $table WHERE $field = ?"); $sth->bind_param(1, undef, SQL_VARCHAR); # or SQL_CHAR? untested. # For numeric types, experiment with SQL_NUMERIC or SQL_INTEGER ... # Now go ahead, DBI will treat any data for the (first) field as a str +ing...
p.s. Disclaimer: this is all based on my memory of experiences of a few years ago. Please don't hold it too much against me if it contains a few minor errors.
|
|---|