ag4ve has asked for the wisdom of the Perl Monks concerning the following question:
it seems that i'm bashing heads with dbi here. i need to bind_columns to an array. now while it seems that this was never meant to happen:
http://www.mail-archive.com/dbi-dev@perl.org/msg03906.html
... and there's probably a more elegant way to solve this in sql itself, i am not good enough with nested select statements to figure it out (or maybe union) and still think that i wouldn't know exactly what i'm returning - hence the array. i've tried the two methods they state here
http://www.perlmonks.org/?node_id=284436
with the same results. here is my (not so pretty) code:
my ($sth, $query, $select, $from, $where, $likedate, $join, $s +elmin, $selmax, $min, $max, @sogs); $key =~ s/\'/\\\'/g; $select = qq/SELECT FLOOR( MIN( n.x ) ), FLOOR( MAX( n.x + ) ) /; $from = qq/FROM o, n /; $where = qq/WHERE MATCH( o.owner, o.mn, o.manag +er ) AGAINST( '+$key' IN BOOLEAN MODE ) /; $likedate = qq/AND n.time LIKE '$date%' /; $join = qq/AND o.key = n.key /; $query = $select . $from . $where . $likedate . $join; $sth = $dbh->prepare( $query ); $sth->execute; $sth->bind_columns( $selmin, $selmax ); while( $sth->fetch ) { if ( !defined( $min ) || !defined( $max ) ) { $min = $selmin; $max = $selmax; } $min = $selmin if $selmin < $min; $max = $selmax if $selmax > $max; } my $countif = ""; for my $i ( $min .. $max ) { $countif .= qq/COUNT( IF( FLOOR( n.x ) = $i,1,null ) ) + /; } $select = qq/SELECT $countif /; $from = qq/FROM o, n /; $where = qq/WHERE MATCH( o.owner, o.mn, o.manag +er ) AGAINST( '+$key' IN BOOLEAN MODE ) /; $likedate = qq/AND n.time LIKE '$date%' /; $join = qq/o.key = n.key /; $query = $select . $from . $where . $likedate . $join; $sth = dbh->prepare( $query ); $sth->execute; $sth->bind_columns( map {$_} @sogs ); while( $sth->fetchrow_arrayref ) { print "$key,"; for( 1 .. $min ) { print ","; } print join( ',', @sogs ), "\n"; }
my error, no matter how i try to return arrays is pretty much the same
Can't DBI::st=HASH(0x1aa7a40)->bind_col(1, undef,...), need a referenc +e to a scalar at /usr/lib/perl5/DBI.pm line 1867, <FILE> line 56.
heh, and it's quite stupid of me to redefine half of my select statement when i don't have to.....
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: dbi bind_columns with an array: fail
by zwon (Abbot) on Oct 18, 2010 at 00:32 UTC | |
by ig (Vicar) on Oct 18, 2010 at 05:30 UTC |