Hi Jerry -

I'm dropping into my pedantic mode; Here's a "baby"
script to access MS Access from Perl:

use strict;
use warnings;
use DBI;

        my $dbh = DBI->connect("DBI:ODBC:MyDatabase")
                || die "could not connect to ODBC:MyDatabase:\n$DBI::errstr\n";

        my $stmt = "select * from NameAddress;";

        my $sth = $dbh->prepare($stmt);
        $sth ||
                die "could not prepare ($stmt):\n$DBI::errstr\n";       

        my $rv = $sth->execute
                || die "could not execute ($stmt):\n$DBI::errstr\n";

    while (my $row_ref = $sth->fetchrow_arrayref) {
        print "$_\t" for (@ { $row_ref } );
        print "\n";
        }

    $sth->finish;
        $dbh->disconnect
                || die "could not disconnect to ODBC:MyDatabase:\n$DBI::errstr\n";

Attached: this script and pod documentation on how to
set up ODBC sources.

Aloha => Beau.

-----Original Message-----
From: Jerry Preston [mailto:g-preston1@;ti.com]
Sent: Wednesday, October 30, 2002 11:20 AM
To: Beginners Perl
Subject: ms access & perl


Hi!!

Is it possible to access MS access from Perl?  If so, how?

Thanks,

Jerry

Attachment: MSAccess.pl
Description: Perl program

NAME

    Accessing MS Acces Databases from Perl.

SYNOPSIS

     use strict;
     use warnings;
     use DBI;

      my $dbh = DBI->connect("DBI:ODBC:MyDatabase")
        || die "could not connect to ODBC:MyDatabase:\n$DBI::errstr\n";

      my $stmt = "select * from NameAddress;";

      my $sth = $dbh->prepare($stmt);
      $sth || die "could not prepare ($stmt):\n$DBI::errstr\n";     

      my $rv = $sth->execute
        || die "could not execute ($stmt):\n$DBI::errstr\n";

      while (my $row_ref = $sth->fetchrow_arrayref) {
        print "$_\t" for (@ { $row_ref } );
        print "\n";
        }

      $sth->finish;
      $dbh->disconnect
        || die "could not disconnect to ODBC:MyDatabase:\n$DBI::errstr\n";

DESCRIPTION

  Set up the ODBC Data Source

    Control Panel -> Administractive Tools
        From My Computer or Windows Explorer, navagate to Asministrative
        tools.

    Data Sources (ODBC)
        Open ODBC Data Source Administrator. Go to System DSN (System DSN
        describes data sources that can be seen by all users logged on to
        your machine.)

    ADD a data source
        Hit Add. You may either Create or Select a database here. Name the
        data source MyDatabase for this test.

    Populate the Database with MS Access
        Open MS Access; create a table NameAddress (for use by this test
        program); create several fields - any type/number; enter a few
        records.

  Test the Perl script

    This script should print your entries. If you have real data you want to
    access, create an ODBC Data Source for it, and modify the connect
    statement in this script:

     my $dbh = DBI->connect("DBI:ODBC:PUT_YOUR_DATA_SOURCE_HERE")

    And the select statement:

     my $stmt = "select * from YOUR_TABLE_NAME;";

WHAT'S NEXT?

    Go for it! Try other SQL statements. Have fun!

AUTHOR

    Beau E. Cox <[EMAIL PROTECTED]>.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to