Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2004
    Posts
    4

    Unanswered: Opening connection to MS Access database in perl

    use OLE;
    $db = CreateObject ADODB connection;
    $db->open("MYDSN");
    .
    .
    .
    .
    $db->close();
    exit(0);

    whenever I try to execute this code. I get an error in Open function.
    Is this way of opening a connection is right. Is there an other way of opening connection to MS Access database.

    Thank You

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Two samples:
    Code:
    #! c:\perl\bin\perl
    
    use Win32::ODBC;
    
    $DriverType = "Microsoft Access Driver (*.mdb)";
    $DSN = "Win32 ODBC --MAOmaoMAOmaoMAO--";
    $Dir = "c:\\meinOrdner";
    $DBase = "meineDB.mdb";
    
    
    Win32::ODBC::ConfigDSN(ODBC_ADD_DSN, $DriverType,("DSN=$DSN", "Description=MAO Win32 ODBC Test DSN for Perl", "DBQ=$Dir\\$DBase", "DEFAULTDIR=$Dir", "UID=", "PWD=")) or die "ConfigDSN(): Could not add temporary DSN" . Win32::ODBC::Error();
    
    $db=new Win32::ODBC($DSN) or die "couldn't ODBC $DSN because ", Win32::ODBC::Error(), "\n";
    
    $query = "select * from table1, table2 where table1.spalte = table2.spalte";
    !$db->Sql($query) or die "couldn't do $query because ", $db->Error(), "\n";
    
    while($db->FetchRow())
    {
     my %Data = $db->DataHash();
     foreach my $key(keys(%Data)){
       print $key," -> ",$Data{$key};
     }  
    }
    
    
    Win32::ODBC::ConfigDSN(ODBC_REMOVE_DSN, $DriverType, "DSN=$DSN") or die "ConfigDSN(): Could not remove temporary DSN because ", Win32::ODBC::Error();
    Code:
    use DBI;
    
    #open connection to Access database
    $dbh = DBI->connect('dbi:ODBC:driver=microsoft access driver (*.mdb);dbq=C:\Windows\Desktop\meineDB.mdb');
    
    #prepare and execute SQL statement
    $sqlstatement="SELECT * FROM table1, table2 WHERE table1.spalte = table2.spalte";
    $sth = $dbh->prepare($sqlstatement);
    $sth->execute || 
          die "Could not execute SQL statement ... maybe invalid?";
    
    #output database results
    while (@row=$sth->fetchrow_array()){ 
       print "@row\n";
    }
    board.perl-community.de - The German Perl-Community

  3. #3
    Join Date
    Oct 2004
    Posts
    4

    got Error

    When I used the second code I got yhe following error:

    D:\Perl\bin>perl db.pl
    Can't locate DBI.pm in @INC (@INC contains: D:/Perl/lib D:/Perl/site/lib .) at d
    b.pl line 1.
    BEGIN failed--compilation aborted at db.pl line 1.

  4. #4
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Then you have to install the DBI-module and the DBD::ODBC-module. These modules are available at http://search.cpan.org ! The included README describes how to install them.
    board.perl-community.de - The German Perl-Community

  5. #5
    Join Date
    Oct 2004
    Posts
    4

    Question Cannot Install DBI!!

    D:\Perl\bin>perl Makefile.PL
    Can't locate Test/More.pm in @INC (@INC contains: lib D:/Perl/lib D:/Perl/site/l
    ib .) at Makefile.PL line 39.
    BEGIN failed--compilation aborted at Makefile.PL line 39.

    I get the following error. I tried installing test::more module, but it also gives error.

    I tried the using win32::ODBC but it also gives following error.

    D:\Perl\bin>perl db.pl
    couldn't ODBC Win32 ODBC --MAOmaoMAOmaoMAO-- because -1024[Microsoft][ODBC Micro
    soft Access Driver] Could not use '(unknown)'; file already in use.

    I am using ActivePerl 5.6.1.633 on Windows 2000.

    Please help...............

    Thank You

  6. #6
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    Since you're using ActiveState, you should use PPM : open a "DOS Box" and enter PPM. From the PPM prompt enter "install DBI", when that comes back, enter "install DBD-ODBC".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •