in reply to parsing $sth-{NAMES}

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

Replies are listed 'Best First'.
Re: Re: parsing $sth-{NAMES}
by fmogavero (Monk) on Mar 24, 2001 at 03:02 UTC
    This is close to what I am looking for. I have been expirimenting all day. The overall story is that I have an object that I will populate the properties with data from the database. I was thinking if I named the properties with the same names for the columns in the tables, I could write a loop that populated each property with corresponding database values.
      My article on www.perl.com goes into some of the techniques, especially the one Masem describes. The last example does just what you say, and it's fairly speedy too. (I haven't benchmarked it, but it uses the fastest retrieval mechanisms together.)

      Wow... I feel like a merlyn or a Dominus. *sniffle*

      The overall story is that I have an object that I will populate the properties with data from the database. I was thinking if I named the properties with the same names for the columns in the tables, I could write a loop that populated each property with corresponding database values.

      There are a number of object-relational technologies for Perl:

      1. Tangram
      2. BingoX::Carbon
      3. DBIx::Recordset
      4. Alzabo
      5. Class::DBI
      6. Class::SPOPS

      It really depends on what you want to do exactly... but the simplest thing is to use DBI as these people said and simply perlfunc:bless the hashref into the desired class.

        Actually, that's just SPOPS rather than Class::SPOPS -- although I'm happy that someone mentioned it :-)

        Chris
        M-x auto-bs-mode