in reply to Re: (jeffa) Re: DBI and MySQL wild card function?
in thread DBI and MySQL wild card function?
Watch closely as I boggle at your statement. Boggle.
Why, er, how are you working with a table where you don't know what the columns are? I can imagine not knowing the column names in advance if you are writing some generic database code that is expected to work with multiple tables or something like that. I included some code form a project I'm working on that demonstrates such a generic function with some PostgreSQL specific bits added in. The only reason I'd expect jeffa to deprecate 'select * ...' is that you can easily chew up memory if you aren't careful. In my case I get back a single row so there's no problem. Message me if you'd like to see the rest of the code.
# #################################################################### +## # Voter::Base Object methods # #################################################################### +## sub populate { # This function is normally used only by object methods. It takes # no arguments and returns a boolean value. It returns false # if the object doesn't actually exist. # 1 / 0 = $object->populate # Object method # # $_[0] = Voter::...=HASH( ... ) $_[0]->ERR_PARAMC unless ( @_ == 1 and ref( $_[0] ) ); # The additional attributes tableoid, oid, xmin, cmin, xmax, cmax, + ctid are # PostgreSQL specific and exist on every row in every table. They +might be # interesting so I'm fetching them as well though any actual use o +f them # should take care to use the values correctly upon consultation w +ith # the PostgreSQL documentation. my $identity = $_[0]->identity_sql('SELECT'); # Don't just execute this inline - I want to keep the $sth handle +around my $sth = $_[0]->dbh->prepare( "SELECT tableoid, oid, xmin, cmin, +xmax, cmax, ctid, * FROM " . $_[0]->CLASS . " WHERE " . $identity->{exp} ); $sth->execute( $identity->{val} ); my $ary = $sth->fetchrow_arrayref; # Return nothing if the object doesn't exist return 0 unless $ary; # Get the list of attribute names and assign their values as a has +h slice @{$_[0]}{ @{$sth->{NAME_lc}} } = @$ary; # that's just assigning an array to a hash slice - except all thro +ugh # references. More prosaicly it might look like # @_{ @attributes } = @values return 1; }
__SIG__
printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE
|
|---|