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

Hi! I recently started playing around with Perl and have just registered with a host who has support for access database. My problem is that they did give me some code but they say it will work using ASP (code is below):
strPathInfo = Request.Servervariables("APPL_PHYSICAL_PATH") strConnection="Driver={Microsoft Access Driver (*.mdb)};DBQ=" & strPat +hInfo & "\_private\db1.mdb" Set objRecset=Server.CreateObject("ADODB.Connection") objRecset.open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & serve +r.mappath(".") & "\_private\db1" sqlstatement="SELECT email, password FROM surya" objRecset.Execute($sqlstatement) objRecset.Close set objRecset=Nothing
Thing is that I need the code which I can just dump in a Perl script which does the same thing. The host does not have a control panel or anything where I can specify the databse DNS...... they want me to make a direct access to the database file on "_private/db1.mdb". How can I do this? Please help. I would really appreciate it. Thanks, SP
  • Comment on Trouble with accessing MS Access database via Perl on web host........
  • Download Code

Replies are listed 'Best First'.
Re: Trouble with accessing MS Access database via Perl on web host........
by repson (Chaplain) on Dec 13, 2000 at 13:52 UTC
    I had a look at the docs for DBD::ODBC and found out that you can use database files just like you are doing (but in perl :). DBD::ODBC is a driver for DBI which is usually the recommended system for dealing with databases because it has supports for anything from flatfile databases (DBD::CSV) to getting data from oracle (DBD::Oracle), and all with the same syntax (allowing you to change between them at will).
    Here's the code that worked for me to connect to a access database file: my $dbh=DBI->connect("dbi:ODBC:driver={Microsoft Access Driver (*.mdb)};DBQ=$filename",'','',{RaiseError => 1}); From there everything else is standard DBI syntax which is easy to find examples of.
      Reply to myself :)
      Might as well add this, what to do after you connect...
      my $sth = $dbh->prepare("SELECT email, password FROM surya"); $sth->execute; my $aref = $sth->fetchall_arrayref; $sth->finish; print "Email\tPassword\n"; for my $row (@$aref) { print join("\t",@$row) . "\n"; }
      That would seem to emulate the rest of the functionality of your code except it doesn't output in HTML, have a look at something like HTML::Template for that.
        To Whom It May Concern. I have tried to use Perl DBI and MS Access together for 2 weeks. But even though I programmed the same code as the code is shown above, it keep telling error message: CGI Error The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are: DBI->connect(driver={Microsoft Access Driver (*.mdb)};DBQ=2000ticket) failed: MicrosoftODBC Driver Manager Invalid string or buffer length (SQL-S1090)(DBD: db_login/SQLConnect err=-1) at d:\inetpub\scripts\find_ticket_test.pl line 19 I really do not know what to do. Please answer my question. thank you
Re: Trouble with accessing MS Access database via Perl on web host........
by $code or die (Deacon) on Dec 13, 2000 at 16:49 UTC
    Hello,

    This should do the trick - you'll need to make sure that the Data Source points to the database on your site. It's using OLE (probably should use Win32::OLE, but I am adapting some old code).
    use strict; use OLE; my $dbh; $dbh = CreateObject OLE "ADODB.Connection" or die "Can't create connec +tion to DataBase: $!" unless $dbh; $dbh->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\_private\\ +db1.mdb"); my $sql = "SELECT email, password FROM surya"; my $rs = $dbh->Execute($sql); while (!$rs->EOF()) { print $rs->Fields('email')->Value . " " . $rs->Fields('password')- +>Value . "\n"; $rs->MoveNext; } $dbh->Close();
    Update: The "Open" string above is using the Access 2000 driver installed with ADO 2.5, you can substitute that line for :
    $dbh->Open("Driver={Microsoft Access Driver (*.mdb)};DBQ=\\_private\\d +b1.mdb");