Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: DBI: when 1 != '1'

by bart (Canon)
on Sep 27, 2002 at 23:55 UTC ( [id://201386]=note: print w/replies, xml ) Need Help??


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 them
But 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
$\ = "\n"; for $a (1, "1") { print $a | " $a"; }
will produce a different result for 1 and for "1".

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://201386]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-20 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found