Thanks, Wayne. I've never tried that with DB connections.

Wayne Simmons wrote:
First off I'd recommend using DBI instead of win32.  In case you don't know
DBI is a generic interface and as impossible, improbable, or ridiculous as
it sounds, you may need to use a different database, different OS, etc. DBI
will ease that transition. Trust me I speak from experience.

Aside from that you could do this:

foreach my $db ($connection1, $connection2)
{
        #Do the stuff you wanna do here.
}

I might add that if you're crashing out with a "die" and I see that you're
trying to be tidy and clean up your connection ($connection->Close()) you'll
need to close both DB to be entirely tidy. That goes for your initial test
if the connection is valid.

Hope that helps.

-Wayne




-----Original Message-----
From: Craig Cardimon [mailto:[EMAIL PROTECTED]
Sent: Wednesday, August 31, 2005 9:46 AM
To: ActivePerl
Subject: Win32::ODBC -- Using two databases simultaneously

I'm connecting to two databases at the same time, test and production. I
have two sets of DSN connections and connection checks:

my $DSN = "Real";
my $DSN2 = "Test";
my $connection = new Win32::ODBC($DSN);
my $connection2 = new Win32::ODBC($DSN2);

## Make sure the connection is valid
if ( ! $connection)
{
    die "\n\tCould not open connection to DSN because of [$!]\n";
}

## Make sure the connection is valid
if ( ! $connection2)
{
    die "\n\tCould not open connection2 to DSN2 because of [$!]\n";
}

*****

I can stick this logic in the beginning of the script.

But when I retrieve data and I trap for errors, I find I need two
different sets of logic, one for test and one for real.

For instance:

my $SQL = "SELECT CustomerID, CompanyName FROM Customers";

if($connection->Sql($SQL))
{
    print "I could not execute the following statement:\n $SQL\n";
    print "I encountered this error:\n";
    print $connection->Error() . "\n";

    ## Closing the database connection
    $connection->Close();

    ## Exiting the program
    die;
}

while($connection->FetchRow())
{
    my @dataRow = $connection->Data();
    print $dataRow[0] . " : " . $dataRow[1] . "\n";
}

From what I've read, I need two each of these sections, one for each
DSN and connection -- one for the Real DB and one for the Test DB.

The IF and WHILE sections above would belong to the Real DB. While I'm
working with the Test DB, I'd need the following:

if($connection2->Sql($SQL))
{
    print "I could not execute the following statement:\n $SQL\n";
    print "I encountered this error:\n";
    print $connection2->Error() . "\n";

    ## Closing the database connection
    $connection2->Close();

    ## Exiting the program
    die;
}

while($connection2->FetchRow())
{
    my @dataRow = $connection2->Data();
    print $dataRow[0] . " : " . $dataRow[1] . "\n";
}

Does this sound correct? I need to work with both DBs at the same time,
and the code's getting a bit sloppy looking. Any suggestions?

-- Craig


---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0535-1, 08/31/2005
Tested on: 8/31/2005 11:45:52 AM
avast! - copyright (c) 1988-2004 ALWIL Software.
http://www.avast.com



_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0535-2, 08/31/2005
Tested on: 8/31/2005 1:39:19 PM
avast! - copyright (c) 1988-2004 ALWIL Software.
http://www.avast.com



_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to