Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Field names of database table from DBI

by plaid (Chaplain)
on Jul 31, 2000 at 21:50 UTC ( #25303=note: print w/replies, xml ) Need Help??

in reply to Field names of database table from DBI

The short answer is, no, there's not a cleaner, portable way to get a list of field names without doing some kind of select statement. If you don't mind sacrificing a little portability, you can use a driver-specific way to do it. For example, in DBD::mysql, you could do
my $sth = $dbh->prepare("LISTFIELDS $table"); $sth->execute; my $names_aref = $sth->{NAME};
Check your appropriate DBD::driver manpage for more specific details if you choose to go this route.

Replies are listed 'Best First'.
RE: Re: Field names of database table from DBI
by jettero (Monsignor) on Aug 01, 2000 at 16:08 UTC
    I'm pretty sure LISTFIELDS doesn't work in my mysql. I've always used this:
    my $s = $h->prepare("show fields from $table"); $s->execute; $s->bind_col(1, \$name); push @names, $name while($s->fetch);
      SHOW COLUMNS FROM $table should also work
      #!/usr/bin/perl # connect to db my $dbh = DBI->connect(bla..bla..bla); my $sql_q = "SHOW COLUMNS FROM $table"; my $sth = $dbh->prepare($sql_q); $sth->execute; while (@row = $sth->fetchrow_array){ print"Field Type Null Key Default Extra\n"; print"----------------------------------------------------\n"; print"$row[0] $row[1] $row[2] $row[3] $row[4] $row[5]\n"; }
        This is database dependant. It won't work for MS SQL through the DBD::ODBC driver, for instance.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2022-01-25 03:03 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (65 votes). Check out past polls.