Calab
Calab

Reputation: 363

Replicating a Perl Oracle database connection in Python

I've been tasked to create a Python script that gets a list of devices from an Oracle database and does some processing on each device. The majority of this is straightforward, but where I am having an issue is connecting to the Oracle database.

I have a Perl script that connects to the same database, but I cannot translate this to the equivalent Python code.

The DBI and DBD::Oracle Perl modules are in use and I am using the cx_Oracle Python module.

Perl is version 5.8.8 and Python is version 2.7.2. This is all running on a CentOS linux box. The Oracle database is not located on the local host.

Here is the pertinent Perl code that does work. The username/password has been changed to protect the innocent.

sub dbConn($$$) {
  # Set Oracle enviroment path information
  $ENV{"LD_LIBRARY_PATH"} = "/usr/lib/oracle/11.2/client64/lib";              
  $ENV{"NLS_LANG"}        = "ENGLISH_CANADA.AL32UTF8";                                              
  $ENV{"ORACLE_HOME"}     = "/usr/lib/oracle/11.2/client64";                  
  $ENV{"PATH"}            = "/usr/lib/oracle/11.2/client64/bin:".$ENV{"PATH"};

  # Get parameters passed to subroutine    
  my $db_pass     = shift();
  my $db_database = shift();
  my $SID         = shift();

  # Connect to db
  my $odbh = DBI->connect("DBI:Oracle:$SID",
    $db_database,
    $db_pass,
    {
      RaiseError => 1,
      PrintError => 1,
      AutoCommit => 0
    }
  ) ||  die("FATAL ERROR:Unable to connect to the Database: $DBI::errstr");

  # Return our database handle
  return ($odbh);



sub dbDisco($) {

    # Get parameter passed to subroutine    
  my $odbh = shift();

  # Disconnect from DB
  $odbh->disconnect;
}  



sub loadDevices {

     # Connect to database
     my $odbh = dbConn( "password", "username", "ENGPRD" );

     # Get device list
     my @devices = dbGetDevices( $odbh );

  # Disconnect from FDB
     dbDisco($odbh);

    return ( @devices );
}

You'll notice that the host is not specified here, but Perl can still find and connect to the database.

How can I find out what host(IP?), port, etc. the Perl script is using? How can I duplicate the Perl dbConn function in python?

Any insight is appreciated.

Upvotes: 1

Views: 296

Answers (1)

Christopher Collins
Christopher Collins

Reputation: 53

The SID 'INGPRD' refers to the database name and instance number. I believe there is a Oracle tnsnames.ora file that identifies these.

I think this link will help

http://www.sap-img.com/oracle-database/finding-oracle-sid-of-a-database.htm

Upvotes: 1

Related Questions