9 gen 2013

Obtain name, type and precision for columns in DBI

I had to find the structure (name, type, and precision for every column) of a table, connected via JDBC using DBI, and this is how I did it:
#!/usr/bin/perl

use DBI;

my $host = '127.0.0.1';
my $url = 'jdbc:Cache://127.0.0.1/SOURCE';
my $dbo = DBI->connect("dbi:JDBC:hostname=$host;port=9001;url=$url", 'username', '***')
  or die $DBI::errstr;

my $qry = $dbo->prepare("select * from custom.table");

$qry->execute();

print "Structure of $table \n\n";

my $num_fields = $qry->{NUM_OF_FIELDS};

for (my $i=0; $i< $num_fields; $i++) {

  my $field = $qry->{NAME}->[$i];
  my $type = $qry->{TYPE}->[$i];
  my $precision = $qry->{PRECISION}->[$i];
  print "$field, $type, $precision\n";
}

$qry->finish();

Nessun commento: