http://qs1969.pair.com?node_id=948305


in reply to Re: DBI and arrays
in thread DBI and arrays

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.

Replies are listed 'Best First'.
Re^3: DBI and arrays
by tweetiepooh (Hermit) on Jan 17, 2012 at 13:27 UTC
    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.
Re^3: DBI and arrays
by Anonymous Monk on Jan 17, 2012 at 12:34 UTC
    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');
      Just wanted to amend that, since you seem to be confused between the database handle functions and the statement handle functions, this would be the equivalent written using the sth functions:
      $sth = $dbh->prepare($sql); $sth->execute('TESTA1', 'TESTA2', 'TESTA3'); $aref = $sth->fetchall_arrayref({stuff => here});
      (the database handle functions offer the do() and select*() shortcuts for doing similar things, although they are not as efficient in some cases)