Re: hashes
by Xen other posts by this author
Oct 19 2000 3:07PM messages near this date
RE: Oracle database usergroup or mailing list reqd.
|
RE: Couldn't write to Ms Access Database by DBD:ODBC
Hello all,
big thanks. Now i know where read about it.
- Mikhail Kashkin (Russia)
Web-master http://www.MaryKay.ru/
-----Original Message-----
From: Steve Sapovits [mailto:SapovitsS@[...].com]
Sent: Thursday, October 19, 2000 12:54 PM
To: Kashkin, Mikhail ; DBI Users
Subject: RE: hashes
You can get a hash reference back using fetchrow_hashref(). The
caveat is that it references the same hash each time so you need
to save results elsewhere (copy to another hash, write them out,
etc.). Example:
my $sql = "SELECT name, age, occupation FROM personnel";
my $sth = $dbh-> prepare($sql);
my $row;
$sth-> execute();
while (defined($row = $sth-> fetchrow_hashref()))
{
print "Name = $row-> {NAME}\n";
print "Age = $row-> {AGE}\n";
print "Occupation = $row-> {OCCUPATION}\n";
}
Note that in this case the column names become upper case
hash keys. That's Oracle behavior but it may vary across
other DB's. I think there are notes on how to deal with this
consistently across DB's in the DBI docs.
Also note (from my experience) that using hashes can add some
overhead to the fetches. For cases where I'm only using them
as a convenience and speed is more important, I instead bind to
specific variables.
> -----Original Message-----
> From: Xen [mailto:xen_ua@[...].com]
> Sent: Thursday, October 19, 2000 4:46 AM
> To: DBI Users
> Subject: hashes
>
>
>
> Hello DBI,
>
> Anybody know how i can recognize name of column from query or
> (better) get it as hash. Of course results be unique only in one
> column.
>
> Example:
>
> SQL - "select name_30, access as Name from users"
>
> name - unique
> access [1,2,3]
>
> while(my(%res) = $sec->fetchrow) {
> %res{'Name'}....
> }
>
> Big thanks.
>
> - Mikhail Kashkin (Russia)
> Web-master http://xen.al.ru/
------------------------------------------------------------------------------
DBI HOME PAGE AND ARCHIVES: http://www.symbolstone.org/technology/perl/DBI/
To unsubscribe from this list, please visit: http://www.isc.org/dbi-lists.html
If you are without web access, or if you are having trouble with the web page,
please send mail to dbi-users-request@[...].org with the subject line of:
'unsubscribe'.
------------------------------------------------------------------------------
|