Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Field names from DBI?

by arthas (Hermit)
on Jun 10, 2003 at 11:16 UTC ( [id://264630]=note: print w/replies, xml ) Need Help??


in reply to Field names from DBI?

adrianh's suggestion is fine. I'll just add that you also have the option to use fetchrow_hashref instead of fetchrow_array: this method returns a refernece to an hash which keys are the field names and values the field values for that row.

Michele.

Replies are listed 'Best First'.
Re: Re: Field names from DBI?
by cchampion (Curate) on Jun 10, 2003 at 14:01 UTC

    They are not the same thing, though.

    Not only fetchrow_hashref will be slower, because the names are assigned for each row, while $sth->{NAME} is only evaluated once, after the call to "execute," but in addition, if your query contains two columns with the same name, $sth->{NAME} will handle it, but fetchrow_hashref will not.

    select * from dept; select * from emp; +--------+----------+ | deptID | name | +--------+----------+ | 1 | pers | | 2 | sales | | 3 | research | +--------+----------+ +-------+-------+--------+ | empID | name | deptID | +-------+-------+--------+ | 1 | John | 1 | | 2 | Fred | 2 | | 3 | Susan | 2 | +-------+-------+--------+ select distinct dept.deptID, emp.deptID from dept left join emp using(deptID); +--------+--------+ | deptID | deptID | +--------+--------+ | 1 | 1 | | 2 | 2 | | 3 | NULL | +--------+--------+

    Given these tables, the Perl code to handle them could be:

    my $query = qq{select distinct dept.deptID, emp.deptID from dept left join emp using(deptID)}; my $sth = $dbh->prepare($query); $sth->execute(); print "--- names\n"; print join ", ", @{$sth->{NAME}}; print "\n--- fetchrow_hashref\n"; my $row = $sth->fetchrow_hashref(); print "$_ \t" for keys %$row; print "\n"; $sth->finish(); __OUTPUT__ --- names deptID, deptID --- fetchrow_hashref deptID

    Notice that $sth->{NAME}, despite the double names, returns the correct result. fetchrow_hashref, instead, misses one column altogether.

      Thanks cchampion,

      Cool, didn't know that... hmmm, /me "thinks have to go redo some code now". I've been working on a bit of code with a friend to 'clone' an SQL table row, you've just given me a couple of very valuable ideas for improvement... thanks again!

      barrd

Re: Re: Field names from DBI?
by barrd (Canon) on Jun 10, 2003 at 11:48 UTC
    arthas is correct, and here is a bit of (untested) code to do exactly that - though there is undoubtly other (better?) ways to do it.

    my $sql = "SELECT * FROM mytable WHERE foo = bar"; my $sth = $dbh->prepare($sql); $sth->execute() || die $DBI::errstr; my $row = $sth->fetchrow_hashref; my %cols = %$row; # Print the field names print join(', ', sort keys %cols); # Print corresponding values print join(', ', map { defined $cols{$_} ? $dbh->quote($cols{$_}) : 'N +ULL' } sort keys %cols);

    Only posted as a guideline...
    barrd

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found