I just try to avoid select * in any serious 'production' type code that i might churn out. Sure, i have been known to be lazy and use select * (i've also been know to use dot star in my regexes!), but i (hopefully) will turn right around and replace the * with the column names. Now, consider this silly but possible example:

Say you have two tables, both with a field named 'id' and a field named 'title':
my $sth = $dbh->prepare(' select * from album,song limit 2 '); $sth->execute(); print Dumper $sth->fetchrow_hashref(); print Dumper $sth->fetchrow_arrayref(); __END__ $VAR1 = { 'album_id' => '1', 'title' => 'Until the End of the World', 'artist_id' => '1', 'year' => '1991', 'id' => '1' }; $VAR1 = [ '2', 'War', '1983', '1', '1', 'Until the End of the World', '1' ];
See the difference? By avoiding select * (or just being happy with an array or array ref) and explicitly specifying the columns, you leave open the option to rename those columns, allowing you to safely store the results in a hash without worry of clobbering existing keys.

So, how do you select the columns when you don't know ahead of time what they will be. Most likely in a vendor-specific way. Here is one way to do it with MySQL:

my @fields = map { $_->[0] } @{$dbh->selectall_arrayref('desc song')}; my $sth = $dbh->prepare(" select @{[join(',',@fields)]} from song "); $sth->execute();

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) 3Re: DBI and MySQL wild card function? by jeffa
in thread DBI and MySQL wild card function? by perleager

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.