Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: DBI and arrays

by Anonymous Monk
on Jan 17, 2012 at 11:59 UTC ( #948305=note: print w/replies, xml ) Need Help??


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)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2022-09-27 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (118 votes). Check out past polls.

    Notices?