Re: How to access MS Access from Perl?

From: Aaron W. West (tallpeak_at_hotmail.NO.SPAM)
Date: 06/04/04

  • Next message: Aaron W. West: "Re: Low memory utilization sort?"
    Date: Fri, 4 Jun 2004 01:59:01 -0700
    
    

    Correct me if I'm wrong, anyone, but...

    I see no way to bind parameters in Win32::ODBC, which means if you wish to
    insert or update values in text fields, you need to build a SQL statement
    while doubling all single-quotes within the fields. Such an approach may not
    be unreasonable, but I think it's preferable to use bound parameters. Also,
    if you were to later migrate to (a heavily loaded) MS SQL Server or Oracle
    as the database, it would be much preferable to use bound parameters, to
    minimize your load on the database server's compiled procedure cache.

    On the other hand, if startup time for your perl script is an issue, and
    performance is less of one, a lightweight module such as this may be
    slightly preferable to the bulkier DBI + DBD::ODBC. In my testing startup
    time is 54 to 70 ms less for use Win32::ODBC; than for use DBI; use
    DBD::ODBC. (Less than the startup time difference between Cygwin Perl and
    ActiveState Perl ; Cygwin perl is faster by around 100 ms with these
    modules.)

    I found that there is a "quote" function in the module DBI::W32ODBC v11.2,
    which also escapes \r and \n characters for Access, so that is apparently
    also necessary. The quote function seems not to be used anywhere in that
    module, so must be provided just for convenience for the user of the module
    for building SQL queries (INSERT or UPDATE statements, in particular.)

    I think DBI and DBD::ODBC are far more mature, as well...

    I really think it would be helpful if the authors of these modules stated
    more of the advantages and disadvantages of using their module right in the
    description of their module. Perhaps it's hard for an author to be objective
    about it. But the module docs for Win32::ODBC don't even give an example of
    an INSERT or UPDATE statement (perhaps a clue that it may be problematic for
    those usages.)

    DBI is admittedly, to me, a little annoying in that it provides too many
    ways to do the same thing. Do I really need all those ways? I'm sure some
    perform better than others, or are better in some situations... Of course,
    there's nothing stopping you from using prepare, optional bind_param for
    each parameter (but do it for all if you do it for any, I think, to be
    consistent), and execute for every query, which is how I did it at first,
    just to minimize the ways I'd have to learn.

    "luican" <luican@yaNooSpammhoo.com> wrote in message
    news:8vJvc.11950$OI5.2480@edtnps84...
    Thanks Aaron. Here's an example I modified using Win32::ODBC and an ODBC
    data source to the Northwind database.
    What benefits will DBI provide?

    lc

    use Win32::ODBC;
        $db = new Win32::ODBC( "northwind" ) || die "Error connecting: " .
    Win32::ODBC::Error();

        if( ! $db->Sql( "SELECT CustomerID FROM customers" ) ) {
            while( $db->FetchRow() ) {
                %Data = $db->DataHash();
                @key_entries = keys(%Data);
                $Row++;
                print "$Row)";
                foreach $key ( keys( %Data ) )
                {
                    print "\t'$key' = '$Data{$key}'\n";
                }
            }
        }
        else
        {
            print "Unable to execute query: " . $db->Error() . "\n";
        }
            $db->Close();

    "Aaron W. West" <tallpeak@hotmail.NO.SPAM> wrote in message
    news:rr2dnWe6vr0NBiDd38DK-w@speakeasy.net...
    > Email to luican@yahoo.com bounces.
    >
    > Anyway, try DBI and DBD::ODBC. Or maybe DBD::ADO, but I've never tried it.
    >
    > ----------------------------------------
    >
    > http://cpan.org
    >
    > http://search.cpan.org/~jurl/DBD-ODBC-1.09/ODBC.pm
    >
    > ..
    > Connect without DSN The ability to connect without a full DSN is
    introduced
    > in version 0.21.
    >
    > Example (using MS Access): my $DSN = 'driver=Microsoft Access Driver
    > (*.mdb);dbq=\\\\cheese\\g$\\perltest.mdb'; my $dbh =
    > DBI->connect("dbi:ODBC:$DSN", '','') or die "$DBI::errstr\n";
    >
    > The above sample uses Microsoft's UNC naming convention to point to the
    > MSAccess file (\\\\cheese\\g$\\perltest.mdb). The dbq parameter tells the
    > access driver which file to use for the database.
    >
    > ..
    > ----------------------------------------
    > One thing you might try is setting the default bind type to varchar as I
    did
    > for MS SQL Server, if you have date conversion errors. But it may not be
    > relevant to MS Access:
    >
    > # This is needed to avoid needless sql_describe_params,
    > # and errors inserting datetime values
    > # It emulates old DBI 0.28 behavior:
    > $dbh->{odbc_default_bind_type} = 12; # SQL_VARCHAR; #12
    >
    > ----------------------------------------
    >
    > http://search.cpan.org/~sgoeldner/DBD-ADO-2.91/lib/DBD/ADO.pm
    >
    > ..
    > The DBD::ADO module supports ADO access on a Win32 machine.
    > ..
    >
    >
    > ----- Original Message -----
    > From: "luican" <luican@yaNooSpammhoo.com>
    > Newsgroups: comp.lang.perl
    > Sent: Tuesday, June 01, 2004 12:07 PM
    > Subject: How to access MS Access from Perl?
    >
    >
    > Hi,
    >
    > Good day! I am writing some Perl script on the server side for HTML
    forms
    > that can create/update an MS Access database. Does anyone know of examples
    > how this can be done?
    >
    > Your help is greatly appreciated.
    >
    > -lc
    >
    >
    >
    >


  • Next message: Aaron W. West: "Re: Low memory utilization sort?"

    Relevant Pages

    • Re: DBD::Informix and SIGBUS in perl
      ... use DBI; ... > My program is a CGI script in perl, which provides web-access to a database ... assigned to a DBD::Informix database handle. ... > LANGUAGE (unset) ...
      (perl.dbi.users)
    • ANNOUNCE : DBIx::DataModel
      ... This is to announce yet another wrapper module on top of DBI, ... Please have a look and see whether it can help suit your needs. ... base DBI layer and to the basic Perl datastructures, ... will not create the database itself. ...
      (perl.dbi.users)
    • Re: Any good Perl coders out there?
      ... I need to convert a PHP script into Perl. ... do is check out the DBI and DBD modules at your friendly neighborhood CPAN ... DBD is the actual database driver for it. ...
      (comp.lang.perl.misc)
    • Re: file parsers
      ... Try DBI and DBD::ODBC. ... Most database programming in Perl is done with the DBI (database ... On Windows, most database programming goes through the ODBC bridge, ...
      (perl.beginners)
    • Any chances to perform parallel (multithreaded) INSERTs?
      ... ithreads in my script so it would be easy to speed up the upload process ... Unfortunately this doesn't work: DBI ... so I cannot let my threads create a database handle individually. ... The database is a Microsoft SQL Server ...
      (perl.dbi.users)