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

Hi, I've got Active Perl 522. Can this be used to write to (for example) an MS Access DB or even a SQL server? And if so ... how? thanks Fiji

Replies are listed 'Best First'.
Re: Writing to a DB in Windows Perl
by Mission (Hermit) on Jun 28, 2001 at 17:09 UTC
    Fiji, the first answer is correct, but I'll explain a bit to get you started. First of all, we like to encourage people to try to help themselves first, it's better for everyone that way. When you get stuck, simply explain as much as you can (where you've searched for the answer, and what the error is) and make sure that you post code examples. This allows us to help you more efficiently (and accurately.)

    First of all, you will need to install the DBI.pm and the DBD-ODBC.pm from CPAN. There are lots of documentation that comes with Perl and with the modules, so specific questions can usually be answered there. Here is a quick code example for hitting an Access DB locally.
    #!/usr/bin/perl -w use strict; #Make sure you always use strict! use DBI; #This implements the DBI.pm # Connect to the Access database called 'db1.mdb' my $DSN = 'driver=Microsoft Access Driver (*.mdb);dbq=db1.mdb'; my $dbh = DBI->connect("dbi:ODBC:$DSN") || die "Couldn't open database +: $DBI::errstr; stopped"; # Prepare the SQL query for execution # From the TEST_TBL return all values for FIELD1,FIELD2,FIELD3 my $SQL1 = $dbh->prepare(<<End_SQL) || die "Couldn't prepare statement +: $DBI::errstr; stopped"; select FIELD1, FIELD2, FIELD3 FROM TEST_TBL End_SQL # Execute the query $SQL1->execute() || die "Couldn't execute statement: $DBI::errstr; sto +pped"; # Fetch each row and print it while ( my ($field1, $field2, $field3) = $SQL1->fetchrow_array() ) { print "Found: $field1 - $field2 - $field3"; } # Disconnect from the database $dbh->disconnect();


    Fiji, I suggest that you get an account here at Perl Monks and participate. It will help you learn more about Perl, and it's a good community to participate with. Good luck.

    - Mission
    "Heck I don't know how to do it either, but do you think that's going to stop me?!!"
Re: Writing to a DB in Windows Perl
by busunsl (Vicar) on Jun 28, 2001 at 14:07 UTC
Re: Writing to a DB in Windows Perl
by holygrail (Scribe) on Jun 28, 2001 at 17:22 UTC
    While everyone is pushing you in the direction of CPAN, which is good of course, you say that you're using ActivePerl. In that case use PPM. You can start it from the dosprompt, by typing ppm. PPM is ActiveState's tool for installing Perl modules.

    You will probably already have DBI and DBD::ODBC as well as Win32::ODBC installed, since Activestate bundles these mostly with their distributions.

    --HolyGrail
Re: Writing to a DB in Windows Perl
by cacharbe (Curate) on Jun 28, 2001 at 17:14 UTC
    Of Course you can.

    use strict; use Win32::ODBC; my $DNSName = <YOURDSNNAMEHERE>; my $insert = new Win32::ODBC("DSN=$DSNName;UID=<DBUSERNAME>;PWD=<P +ASSWORD>") || die "Error: " . Win32::ODBC::Error(); my $sqlstr = "INSERT INTO....<BLAH><BLAH>etc."; if (!$insert->Sql($sqlstr)){ #Do Something }else{ #Figure out the error and do something else }

    Of course, you should be typing you question into the super search, which would give you a number answers, including this one that has links to a couple of tutorials (care of crazyinsomniac) Here and here .

    I suggest you go to a few of these, read and learn.

    UPDATE: I took the time to offer a few links, and add use strict;. I was in a hurry earlier. *sigh* no rest for the wicked.

    C-.