in reply to Re: Quote-like operators in a hash
in thread Quote-like operators in a hash

I used,
while(my($key,$value)=each(%sqlASDOverall)) { print sqlExecute($value,$key) }

to loop over the hash. I printed out the SQL query, and it looks to be formatted correctly. This is the SQLExecute subroutine I use to run sql queries.
sub sqlExecute { my @results; my $sth = $dbh->prepare($_[0]); $sth->execute() || die print "Could not execute SQL query :\n$_[0] +\n",$sth->erstr(),"\n"; while (my $result = $sth->fetchrow_array()) { push @results, $result; } return @results; }

Replies are listed 'Best First'.
Re^3: Quote-like operators in a hash
by Rodster001 (Pilgrim) on Jan 05, 2009 at 18:49 UTC
    Try simplifying the statements the hash is passing by doing something super simple like, "SELECT NOW()" and see if that works. I would also add a debugging line just about the prepare call and print out the statement (just to make sure everything looks ok). It looks like that is where your problem is, for prepare to die with that error is weird. Although, I have never used the ODBC driver with DBI but I've never seen that error with the MySQL driver.
Re^3: Quote-like operators in a hash
by Bloodnok (Vicar) on Jan 05, 2009 at 17:49 UTC
    ...I might be missing something, but it rather looks like prepare might be expecting a quoted string - have you tried my $sth = $dbh->prepare("$_[0]");, or similar ?

    Just a thought ...

    A user level that continues to overstate my experience :-))
      Your reply got me thinking on the right track. for some reason, when I did,
      print $key." : ".sqlExecute($value);

      something went wrong and it returned 1's instead of the query result. It seems like Perl didn't like the concatenations. I re-did the print to,
      print "\t\t".$key." : "; print sqlExecute($value); print "\n";

      I'm not sure why that changed things, but it did. If anyone knows why, I would like to hear it. Thanks to all who helped. I wouldn't have figured it out with your help.

        The difference is that print imparts a list context, but the dot operator creates a scalar context. Your sqlExecute() ends with "return @result". An array in a scalar context evaluates to the number of items in the array, but in a list context, it's all the elements of the array.

        Update: Here are a couple ways to get what you want without three lines:

        • Use commas: print "\t\t".$key." : ", sqlExecute($value), "\n";
        • Interpolate an anonymous array reference: print "\t\t$key : @{[ sqlExecute($value) ]}\n";

        The first is better for print, but the second can be useful in other places where you want to interpolate a function call.