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

I want to connect to an Access 2000 file using perl DBI. The database is db1 and the table is table1. The DNS I set up is fred3. What is the proper code for connection?

Replies are listed 'Best First'.
Re: Perl and Access 2000
by NetWallah (Canon) on Oct 28, 2004 at 00:39 UTC
    Sample code (Untested):
    use strict; use DBI; my $dbh =DBConnect(); my $sth = $dbh->prepare("SELECT * FROM WHATEVER"); ################ sub DBConnect{ # Connect to the database my $CONNECTIONSTRING = "DSN=mySystemDSN;" . "Uid=myUsername;" . "Pwd=myPassword" ; return DBI->connect("DBI:ODBC:$CONNECTIONSTRING", '','', # Username and password # Now set the DBI Attributes ... {RaiseError => 1, # do this, or check every call fo +r errors ChopBlanks=> 1, LongTruncOk =>1, LongReadLen => 25 +} ) or die "Couldn't open database:'$CONNECTIONSTRING'\n $DBI::err +str; stopped"; }
    btw - it is "DSN", not DNS.

        Earth first! (We'll rob the other planets later)

Re: Perl and Access 2000
by strat (Canon) on Oct 28, 2004 at 06:14 UTC

    With Access, you can also use a filename as a "DSN" and don't need to use ODBC-Manager but handle it from the script, e.g. from perldoc DBD::ODBC

    # as UNC (beware the proper rights) my $DSN = 'driver=Microsoft Access Driver(*.mdb);dbq=\\\\cheese\\g$\\p +erltest.mdb'; # or just as plain filename: my $DSN = 'driver=Microsoft Access Driver(*.mdb);dbq=g:\\perltest.mdb' +; my $dbh = DBI->connect("dbi:ODBC:$DSN", '','') or die "$DBI::errstr\n";

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: Perl and Access 2000
by jZed (Prior) on Oct 28, 2004 at 00:50 UTC
    Use DBD::ODBC. Make sure that you created fred3 as a *system* DNS. Then connect like this: my $dbh = DBI->connect('dbi:ODBC:fred3'); update oops, typos are catching. as another poster pointed out, it's DSN, not DNS. :-)