The DBI defines methods for getting metadata. If you want to get info on a given table, though, you have to go through a statement handle. It's a little bit involved after that. You execute a "select * from table_name" (or, if you know the field names you're interested in, just get those) and then the driver makes other info available to you via the {NAME} and {TYPE} attributes of the statement handle.
The *statement handle* attribute {NAME} is a reference to an array of the names of the fields returned by the current statement, and there's also the Probably the most straightforward way of doing this looks something like this:
# you've already got your DB handle:
my @tables = $db->tables();
foreach my $table (@tables) {
my $sql = "select * from $table";
my $sth = $db->prepare( $sql );
$sth->execute(); # check for errors, either set RaiseError or have
+ a "die" clause here
print "Structure of $table \n\n";
my $num_fields = $sth->{NUM_OF_FIELDS};
for ( my $i=0; $i< $num_fields; $i++ ) {
my $field = $sth->{NAME}->[$i];
my $type = $sth->{TYPE}->[$i];
my $precision = $sth->{PRECISION}->[$i]; # e.g. VARCHAR(50) ha
+s a precision of 50
print "Field $field is of type $type, with precision $ precisi
+on\n";
}
$sth->finish();
}
You can turn those numeric types into more descriptive names, I don't know anything specifically about DBI::Pg, and my knowledge is derived from the very useful Programming the Perl DBI, which has been surpassed on the metadata issue via newer versions of the DBI, but I see that those newer versions of the DBI define type_info methods that can help you figure out which types you're dealing with.
HTH.
I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|