Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How do you get Perl::DBI to do a desc table_name?

by vek (Prior)
on Mar 27, 2002 at 18:59 UTC ( [id://154766]=note: print w/replies, xml ) Need Help??


in reply to How do you get Perl::DBI to do a desc table_name?

You can try the NAME and the TYPE DBI statement handle objects.

Here's how you can grab column names:
my $names = $sth->{NAME}; #$names is an array reference for (@$names) { print $_, "\n"; }
Here's how you can grab the datatype of the columns:
my $types = $sth->{TYPE}; #$types is an array reference for (@$types) { print $_, "\n"; }
The last example returns an array of integer values, the value indicates the datatype (see ANSI X3.135 for a description).

Replies are listed 'Best First'.
Re: Re: How do you get Perl::DBI to do a desc table_name?
by lachoy (Parson) on Mar 28, 2002 at 13:23 UTC

    Two minor notes:

    First, you might want to be careful about the values returned by $sth->{NAME} if you care about case-sensitivity. The DBI doesn't make them consistent or modify them at all -- they're whatever the database returns. And what the database returns can be vendor specific. The NAME_uc and NAME_lc properties do what you'd expect.

    Second, I think the integer values returned from $sth->{TYPE} correspond to the DBI SQL type constants. So you can do:

    use DBI qw( :sql_types ); ... my $types = $sth->{TYPE}; foreach my $type ( @{ $types } ) { print "VARCHAR" if ( $type == SQL_VARCHAR ); print "DATETIME" if ( $type == SQL_DATETIME ); print "BOOLEAN" if ( $type == SQL_BIT ); print "FLOAT" if ( $type == SQL_FLOAT ); ... }

    Conveniently, most of them also map to the ODBC CLI specifications. :-)

    You can find the type-to-constant mapping in the 'dbi_sql.h' header file found in your architecture-dependent perl library tree -- for example, mine is in site_perl/5.6.1/i686-linux/auto/DBI/dbi_sql.h

    Chris
    M-x auto-bs-mode

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (None)
    As of 2024-04-25 00:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found