thunders has asked for the wisdom of the Perl Monks concerning the following question:

I've been working with DBI recently, and I've been attempting to automate some tedious tasks with it. For example from time to time I have to convert MSAccess database tables to Oracle tables. The Method my teacher gave the class(yes this is homework) involves too many repetitive steps for my tastes.

What I want to do is scan the access DB and create Oracle tables on the fly. So first thing that I needed was info about what datatypes I'm dealing with for each column.

#!/usr/bin/perl -w use strict; use DBI; #connect to Access file via ODBC my $accessDSN = q(driver=Microsoft Access Driver (*.mdb);). q(dbq=D:\\Course 1 Case Study Files\\Order Entry Syste +m.mdb); my $dbhA = DBI->connect("dbi:ODBC:$accessDSN",'','') or die "$DBI::err +str\n"; #prepare handles for each table. my @tables = qw(Customers OrderLineItems Orders Products); my @tblSth = map {prepare($_,$dbhA)} @tables; #print column data for each table for my $sHandle(@tblSth){ $sHandle->execute; my $index = (keys %{$sHandle->fetchrow_hashref("NAME")}); for my $col(0..$index){ print join " ",$sHandle->func($col,"DescribeCol"), "\n"; } } sub prepare{ my ($table,$dHandle) = @_; return $dHandle->prepare("select * from $table"); }

This is a snippit of output of that script

CustId 4 10 0 0 FirstName 12 50 0 1 LastName 12 50 0 1 CompanyName 12 50 0 1 Address1 12 50 0 1 Address2 12 50 0 1 City 12 50 0 1 State 12 2 0 1 Zip 12 10 0 1 Phone 12 25 0 1 Fax 12 50 0 1

does any one know where the "DescribeCol" function is documented? I can tell that the first list item returned is of course column name, and the second is data type, and the third decribes size, but I do not know what the fourth and fifth columns describe. and I dont know how to decode the datatype...

Is there a better way to get the Datatype of a column?

Replies are listed 'Best First'.
Re: finding field infomation with DBI and ODBC
by dws (Chancellor) on Jan 14, 2002 at 01:35 UTC
    does any one know where the "DescribeCol" function is documented?

    If you're running ActiveState Perl, it's in the documentation.   Start / Programs / ActiveState ActivePerl / Documentation Launches a browser on their documentation index. Scroll the left column down to DBI. In that page, the section "Statement Handle Attributes" covers the functions you need. The DBI API covers in several function what ODBC covers in one. This makes the API portable across more native database APIs.

      I had already checked the DBI Documentation as well as the DBD::ODBC docs. I think I realize the problem. The function I was using is defined in ODBC documentation. Which I just found online here I'll try to do this via the functions you mentioned. Thanks.
Re: finding field infomation with DBI and ODBC
by CubicSpline (Friar) on Jan 14, 2002 at 18:38 UTC
    You may have already found your answer, but I'm almost certain that the last two values are boolean values which represent, respectively, "Is this column a primary key?" and "Can this column accept NULL values?"

    ~CubicSpline
    "No one tosses a Dwarf!

      You were half right.
      From the ODBC documentaion:

      DecimalDigits. The number of decimal digits of the column on the data source. If the number of decimal digits cannot be determined or is not applicable, the driver returns 0.

      Nullable. Indicates whether the column allows null values.

      Primary key information would be at least as useful to me as decimal digits. I'm guessing that decimal digits are the second argument to the number(,) datatype? In fact because of this, It doesn't seem like I'm going to be able to fully automate this process.

      After over an hour of looking through ODBC and Access Documentation I still can't figure out exactly what datatypes map to which integers in the second column. It's maddening. I'm still working on a solution to this.