in reply to Having issues with DBI
(Update: your problem was the package, ho-ho: Re^5: Can insert but not select. I took the PP in the mysqlPP in your sample to be a typo. Silly me.)
Your problem is bad code somewhere, not a DBI + SELECT issue. This is one of those moments I go through sometimes where I'm dead certain there is something mysterious going on but in fact Occam is right: your code is wrong; not the code of 100,000 other devs. :)
Is this really all you're running?
$sth->execute($color);That will execute but it won't print anything and you're not capturing or testing its results. You need to do something with the statement handle after its executed or at least check its return value. Try this or a near variant-
use strict; use warnings; use DBI; my $dbh = DBI->connect("DBI:mysql:foo;mysql_read_default_file=~/.my.cn +f", undef, undef, { AutoCommit => 1, RaiseError => 1, } ); my @color = qw( red blue green puce majenta lavender lake teal ); my $sth_color_exists = $dbh->prepare("SELECT 1 FROM color WHERE color += ?"); # uncomment to get lots of info about what's going on. # DBI->trace(2); for my $color ( @color ) { $color =~ s/^\s+|\s+$//g; my $rv = $sth_color_exists->execute($color); printf(qq{There %s %d row%s found for color = "%s"\n}, $rv == 1 ? "was" : "were", $rv, $rv == 1 ? "" : "s", $color ); } $sth_color_exists->finish();
Note that the statement handle only has to be created once and reused as much as you like. I personally would create hash of the colors with one big query instead. Something like this thought it's a little tricky-
my $array_ref = $dbh->selectcol_arrayref("SELECT color, 1 FROM color", { Columns => [1,2] } ); my %colors = @{ $array_ref }; # Now rewrite the loop above along the lines of- # for my $color ( sort keys %colors )
|
|---|