[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mdbtools-dev
Subject:    Re: [mdb-dev] MDB-Tools and Perl
From:       "Kyle R. Burton" <mortis () voicenet ! com>
Date:       2003-08-04 13:17:55
[Download RAW message or body]

> I am curious as to the ways in which one can interact with mdb-tools from 
> within a perl script?  I am not a strong programmer.  I can use the system 
> command from perl to start the command line utilities but can not pass it the 
> required information.  Does anyone have much experience of working with both 
> MDB tools and perl?

We have used MDB tools from Perl to list the tables in an MDB (using 
mdb-tables), and get all the records from a table (using mdb-export).

We either used backticks or open to read from those commands.  A direct call
to the C functions (libmdb) would probably be a better long teram solution
(DBD::mdbtools would be even better).

Pseudo code for we're using looks like:

# returns an array of table names
sub list_tables
{
    my($fileName) = @_;
    my $cmd = "mdb-tables $fileName";
    my $output = `$cmd`;
    if( 0 != ($? >> 8) ) {
        die "Error obtaining tables list [$cmd]: $output : \$!=>$! : \$?=>$?";
    }
    my @lines = split(/[\s\r\n]+/,$output);
    return @lines;
}

# returns an array of arrayrefs (array of records)
sub get_records
{
    my($fileName,$tableName) = @_;
    my $cmd = "mdb-export -d'\t' -Q $fileName '$tableName' |;
    my $fh;
    unless( open( $fh, $cmd ) ) {
        die "Error opening mdb read handle: $! : cmd: '$cmd'";
    }
    my @lines = <$fh>;
    close $fh;
    my @records;
    foreach my $line ( @lines ) {
        my @fields = split('\t',$line);
        $fields[-1] =~ s/[\r\n]+//;
        push @records, \@fields;
    }
    return @records;
}



-- 

------------------------------------------------------------------------------
Wisdom and Compassion are inseparable.
        -- Christmas Humphreys
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------


-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
_______________________________________________
mdbtools-dev mailing list
mdbtools-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mdbtools-dev
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic