Обсуждение: simple query question to use with DBI selectall_hashref

Поиск
Список
Период
Сортировка

simple query question to use with DBI selectall_hashref

От
Kenji Morishige
Дата:
I would like to create a query that returns a column with an integer
1 through (row_count) to use as the index while used in conjunction with
DBI's selectall_hashref($sql,$key) function.  In the past I'd usually just
write a wrapper around selectrow_hashref and put all those results in an
array. I don't know why DBI doesn't include a function like this.

if I could extract a pseudo column from the query itself with the result
order number, I could just use that as the key in selectall_hashref and sort
the results by key.

Is there a built-in function that returns the current row #?

select foo from bar;
row#;foo
----;-----
1;foodata1
2;foodata2
3;foodata3
4;foodata4

so I can pump it into:
my $hashref = $dbh->selectall_hashref($query);

print $hashref->{1}; # prints 'foodata1' etc

Or am I missing something and there is a way better way to do this...?

Sincerely,
Kenji

Re: simple query question to use with DBI selectall_hashref

От
Martijn van Oosterhout
Дата:
On Thu, Jul 05, 2007 at 01:39:31PM -0400, Kenji Morishige wrote:
> I would like to create a query that returns a column with an integer
> 1 through (row_count) to use as the index while used in conjunction with
> DBI's selectall_hashref($sql,$key) function.  In the past I'd usually just
> write a wrapper around selectrow_hashref and put all those results in an
> array. I don't know why DBI doesn't include a function like this.

I think DBI already does what you want, from the docs:

           You may often want to fetch an array of rows where each row
           is stored as a hash. That can be done simple using:

             my $emps = $dbh->selectall_arrayref(
                 "SELECT ename FROM emp ORDER BY ename",
                 { Slice => {} }
             );

It's then an array rather than a hash, but that's better, right?

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Вложения