selectall_hashref
mnlight
created: 2006-01-04 17:42:01
I came across the selectall_hashref by way of some advice I recieved on a previous post.
It looks like something that might work for a script I am writing.
I am trying to test it and learn exactly how it works.
I wrote this code
my ($k, $v);
my %data_all;
my $dbh =  DBI->connect('DBI:Sybase:#####', '#####', '#####', { RaiseError => 1, AutoCommit => 1});
my $sql = "select Col1, Col2, Col3 from TABLE where Col4 = 1";
my $data_all = $dbh->selectall_hashref($sql, 1);

while ( ($k,$v) = each %data_all ) {
    print "$k => $v\n";
}
I get this error.

DBI::st=HASH(0x1027b0)->_prepare(...): attribute parameter '1' is not a hash ref at /nms/site/perl-5.6.0/lib/site_perl/5.6.0/sun4-solaris/DBD/Sybase.pm line 95.

The document I read said that the second parameter is "the column to use as key, indicated by position".
I want Col1 to be the key.
Re: selectall_hashref
created: 2006-01-04 17:56:57
The document I read said that the second parameter is "the column to use as key, indicated by position".

What document was that? Look at the docs for fetchall_hashref, it works the same way (except you have to call prepare and execute), and has an example:

  $dbh->{FetchHashKeyName} = 'NAME_lc';
  $sth = $dbh->prepare("SELECT FOO, BAR, ID, NAME, BAZ FROM TABLE");
  $sth->execute;
  $hash_ref = $sth->fetchall_hashref('id');
  print "Name for id 42 is $hash_ref->{42}->{name}\n";
Update: I do also see this for fetchall_hashref, so you may be right (in some other version of documentation from mine), but try it both ways:
The $key_field parameter can also be specified as an integer column number (counting from 1).
Update: nevermind...see correct answer below.
Re: selectall_hashref
created: 2006-01-04 18:01:00
try while ( ($k,$v) = each %$data_all ) {
Re^2: selectall_hashref
created: 2006-01-04 18:22:42
Still not working.
If I remove the second parameter selectall_hashref($sql) I recieve this error
"Bad index while coercing array"
With a second parm, either col location or col name I get the original error.
Re: selectall_hashref
created: 2006-01-04 18:32:39
What is "1" as the second parameter to selectall_hashref()? That parameter should be the name of the column you want to use as a key, e.g. 'Col1', not an integer.
Re^2: selectall_hashref
created: 2006-01-04 18:35:18
I have tried that and it has the same result. The doc I read said you could indicate the column by location.
Re: selectall_hashref
created: 2006-01-04 18:57:24

I hate to be one to blame bugs in the system, but in this case it looks like it might be a bug in DBD::Sybase because I tried essentially the same code with DBD::Oracle and did not get an error like that. Make sure you have upgraded to the latest versions. Then try it using a named column as the index indstead of a number, just to better isolate the problem. If this doesn't fix it you might want to report the bug. (note that mpeppler is something of a regular here)

However, please also follow smokemachine's advice because otherwise once this error is dealt with you fill still find that you don't have any data being returned.

Re^2: selectall_hashref
created: 2006-01-05 02:12:53
It's not a bug in DBD::Sybase - I just tried this and it worked for me.

So I'm guessing (as others have) that the OP has an old version of DBI.

Michael

Re: selectall_hashref
created: 2006-01-04 19:59:18
Have you tried breaking it down and using fetchall_hashref instead? EG:
my $sql = "select Col1, Col2, Col3 from TABLE where Col4 = 1";
my $dbq = $dbh->prepare($sql);
$dbq->execute or die "Could not execute $sql:$!\n";
my $ref = $dbq->fetchall_hashref('Col1');
Re^2: selectall_hashref
created: 2006-01-04 23:52:07
Just tried doing the fetchall_hashref and got this error. "Can't locate object method "fetchall_hashref" via package DBI::st"
Why does this stuff always happen to me?
Re^3: selectall_hashref
created: 2006-01-05 00:53:39
Which version of DBI do you have?

From perldoc DBI::Changes, it appears that support for the fetchall_hashref method was added in version 1.20 (August 2001). If you don't have at least this version then you may want to grab the latest version.

Cheers,
Darren :)

Re^3: selectall_hashref
created: 2006-01-05 00:59:27
What version of DBI do you have?
Re^4: selectall_hashref
created: 2006-01-05 01:31:18
I don't know how do I determine it.
Re^5: selectall_hashref
created: 2006-01-05 01:39:59
Either find DBI.pm and look at the source or try this on the command line:
perl -MDBI -e 'print $DBI::VERSION . "\n"'
Re^5: selectall_hashref
created: 2006-01-05 01:41:42
perl -MDBI=9999
Re^3: selectall_hashref
created: 2006-01-05 02:13:51
Why does this stuff always happen to me?

Trust me - you're not the only one...

Re: selectall_hashref
created: 2006-01-05 01:32:31
When I pull a query like that, I normally do it like:
my $dbh =  DBI->connect('DBI:Sybase:#####', '#####', '#####', { RaiseError => 1, AutoCommit => 1});
my $sql = "select Col1, Col2, Col3 from TABLE where Col4 = 1";
my $data_all = $dbh->selectall_arrayref($sql, { 'Columns' => {} } );

for my $row ( @$data_all ) {
  print "$_ => $row->{$_}\n" for keys %$row;
}
Will that do what you are looking for.

Don
WHITEPAGES.COM | INC
Everything I've learned in life can be summed up in a small perl script!
Re^2: selectall_hashref
created: 2006-01-05 01:43:54
I resorted back to my old way. Thank you for your help.

perlmonks.org content © perlmonks.org and BaldPenguin, Errto, jZed, McDarren, mnlight, mpeppler, perrin, runrig, smokemachine, spiritway

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03