1

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.

2
  • The host etc. is passed as arguments when the subroutine is invoked. For python refer to this stackoverflow.com/questions/372885/… Commented Apr 24, 2017 at 7:45
  • Thanks. My biggest problem though, is that I don't know the host. Perl doesn't seem to need it. It uses DBI:Oracle:ENGPRD to make the connection. How would I translate this to a host for use in Python? Commented Apr 24, 2017 at 17:24

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! The host name was located in the tnsnames.ora file. I now have my python script working!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.