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:

use DBI;

my $host = '';
my $url = 'jdbc:Cache://';
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");


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";


Nessun commento: