If you are using the DBI interface, there is the
$sth->fetchrow_hashref function which returns a hash reference with the field names as keys, and the appropriate db values as the hash values. But this is
very s l o w.
You should always try to get a subselection of the fields from the table if you can (e.g. don't use SELECT *). If you know what fields you want in array @fields, then you can set up your select statement as
$sth->prepare( 'SELECT ' . join( ',', @fields ) . ' FROM database ETC
+ETC ETC' );
...then use fetchrow_array as follows...
%rowhash{ @fields } = $sth->fetchrow_array();
(whereby you are using an hash slice to make everything neat and tidy without too much extra work). However, be aware that even this way will probably be faster than fetchrow_hashref, but still on the slow side if you have lots of data. Note that if you MUST use SELECT *, or at least, you are trying to grab all the fields, you can query via DBI all the fields for a given table, put those into @fields, and use that for further SELECTS.
Also take a look at node Tricks with DBI, which tell you other things to avoid in this case.
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.