Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

DBI and arrays

by Anonymous Monk
on Jan 17, 2012 at 10:55 UTC ( #948293=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm stuck with DBI and arrays of bind variables, I've read the documenatation and I'm still just as confused. Hopefully someone here will help.
select cola, colb, colc from table-name where colx=? and coly=? and colz=?"
I have an array containing the values of colx,y and z and I want to run this query for each of these sets of values. Then I want to store the results from all these queries in an array. Here is my perl effort
my @Queryresults = @{ $sth->selectall_arrayref( $SQLQueryString, @SQLB +indVars) };
Where @SQLBindVars is an array containing my bind variables like I described above. The result in my error_log is
Can't bind a reference (HASH(0x92705c))
Can anyone show me the correct way of doing this, please lt me know if my question is not clear.

Replies are listed 'Best First'.
Re: DBI and arrays
by moritz (Cardinal) on Jan 17, 2012 at 11:05 UTC
    From the fine docs:
    $ary_ref = $dbh->selectall_arrayref($statement, \%attr, @bind_values);

    So selectall_arrayref expects a hash reference as second argument, which you didn't pass. If you don't want to pass any %attr's, just pass undef (or if that doesn't work, a reference to an empty hash, {}) as second argument.

      Using Data::Dumper I print @SQLvars like so, before executing the query
      warn Dumper \@SQLvars;
      I see the array printed, with 4 items in each row of the array, like this
      $VAR1 = [ { 'COLX' => 'TESTA1', 'COLY' => 'TESTA2', 'COLZ' => 'TESTA3' }, { 'COLX' => 'TESTB1', 'COLY' => 'TESTB2', 'COLZ' => 'TESTB4' },
      I then execute the query
      my $sth = $dbh->prepare($SQLQueryString); my $array_ref = $dbh->selectall_arrayref( $SQLQueryString, { Slice => +{} }, @SQLvars );
      Which returns this error
      DBD::Oracle::db selectall_arrayref failed: called with 9 bind variable +s when 3 are needed
      For the life of me I can't work out why it thinks there should be 9. If you or anyone else can advise that'd we great.
        There seems to be some confusion here about what you are passing. @bind_values needs to be an array of values not an array of hashes or other structure and the Slice is how the returned data is handled. Me thinks that your code needs to be more like
        my @param = <array of values to pass TO database>; my $sql = <some SQL with placeholders = ? to take values>; my $array_ref = $dbh->selectall_arrayref($sql,undef,@param);
        If you need to run the query for multiple sets of parameters you are better off preparing the SQL and then fetching the data multiple times with each set of parameters.
        my $dbh = <create database handle>; my $sth = $dbh->prepare("select * from table where col1=? and col2=?") +; ... @param = ("test1a","test1b"); $array_ref = $sth->selectall_arrayref($sth,undef,@param); ... @param = ("test2a","test2b); $array_ref = $sth->selectall_arrayref($sth,undef,@param); ...
        The use of statement handle in the selectall_arrayref is not something I've used yet but is in the DBI docs.
        my $sth = $dbh->prepare($SQLQueryString); my $array_ref = $dbh->selectall_arrayref( $SQLQueryString, { Slice => +{} }, @SQLvars );
        First of all, you are preparing a statement handle, and then not using it (but using the database handle instead). Second, DBI does not expect named parametres (at least the way you're writing your placeholders), nor will that function accept an array of hashrefs. Instead, something like this would work:
        $aref = $dbh->selectall_arrayref($sql, {stuff => 'here'}, 'TESTA1', 'T +ESTA2', 'TESTA3');
      Thanks I'll give this a shot, I didn't intend to insult the docs for dbi, just my inability to understand them

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2022-09-30 15:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (126 votes). Check out past polls.

    Notices?