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

Hi, I'm a newbie. I want to connect to the remote oracle database using perl. I tried using DBD::Oracle but everytime I try testing my script it crashes my perl interpreter and I get an error message saying "Premature end of script headers". Don't know what is wrong? Can I use DBD::ODBC for this purpose? Can anyone tell me how to connect to remote oracle database using perl? I'm working on Windows XP. My script is: -
use CGI; use strict; use DBI; use CGI::Carp qw(fatalsToBrowser); # gives detailed errors my $hostname= "sample.oracle.com"; my $sid="MYSID"; my $port = "1521"; my $username = "user"; my $password = 'pass'; my $dbh; $dbh = DBI->connect("dbi:Oracle:host=$hostname;sid=$sid", $username, $ +password) or die "connect failed: ". DBI->errstr() unless $dbh;
Thanks

Replies are listed 'Best First'.
Re: perl remote oracle database connection
by Tobin Cataldo (Monk) on May 24, 2006 at 22:33 UTC

    a couple of things to try

    finish what you are doing. if you are writing a html page, send your html headers with a 'yay' or 'nay'. Write the whole query, i.e. connect, prepare, execute, disconnect. Then you should be able to find out where it is failing.

    my quick two minute below

    #!/usr/bin/perl use strict; use warnings; use DBI; use CGI; $ENV{ORACLE_HOME} = "/disk2/oracle/product/10.1.0/db_1"; #declare your ORACLE_HOME in the script... (not the problem here but i +t could become a problem later on) my $dbh = DBI->connect('dbi:Oracle:host=host.domain.tld;sid=SID;port=1 +521', 'USER', 'PASS', { RaiseError => 1, AutoCommit => 0 }); print "Content-type: text/html\n\n"; my $sth0 = $dbh->prepare("SELECT...") or die "Couldn't prepare 1st statement: " . $dbh->errstr; $sth0->execute; while (my @data = $sth0->fetchrow_array()) { my $column1 = $data[0]; my $column2 = $data[1]; square brackets here print $column1 . "\t" . $column2; } $sth0->finish; $dbh->disconnect;

    Tobin

    Code tags added by GrandFather

Re: perl remote oracle database connection
by jZed (Prior) on May 24, 2006 at 22:26 UTC
    That's a CGI error, not a perl or DBI error. It means you didn't print a content-type header which you can easily do with print CGI::header().
Re: perl remote oracle database connection
by srdst13 (Pilgrim) on May 25, 2006 at 02:18 UTC

    When trying new things in perl, I typically start with the simplest possible code and work up from there. In this case, I wouldn't put all of this in a CGI environment before troubleshooting your database code. In the case of your error, it is NOT from your database call, but is a CGI error that arises from not supplying appropriate header information to the browser. Work on the two pieces separately until you have a working "hello world" CGI script and a simple connection and pull data from the database. Once you have the two working, then you can combine them.

    Just my opinion, though.

    Sean

      I wrote simple perl scripts and they run fine. In my script I get the "premature end of script" error the moment I add this line
      my $dbh = DBI->connect('dbi:Oracle:host=host.domain.tld;sid=SID;port=1 +521', 'USER', 'PASS', { RaiseError => 1, AutoCommit => 0 });
      Is this some issue with the DBD::Oracle????
      This is what I get: -
      Server error! The server encountered an internal error and was unable to complete your request. Error message: Premature end of script headers: connect-test.pl
      If you think this is a server error, please contact the webmaster. Error 500

      I really don't know whats the issue. I tried setting oracle environment variables in the script but no help.
      Please help!!!!
        Are the simple perl scripts CGI or Oracle connection scripts?

        The first thing you should do is run your oracle connect on the command line until you know it is working right (without the cgi)... That way atleast you will get STDERR.

        Question two: do you have the Oracle client installed?
        I seem to remember that Oracle-DBD by itself is no good. You need to also install the Oracle client and get it configured to connect to a remote Oracle Database through such application as SQLPLUS. Then install Oracle-DBD.
        (Its been awhile since I have done anything with this so things might have changed... I just remember Oracle being the complete antithesis of a simple MySQL connection string.)


        You should also check your cgi configuration to make sure you are sending appropriate headers etc from Apache. This could be done by making a simple hello world test using the <h1>Hello World</h1> syntax.


        When both pieces prove to work independently of each other, then you can combine them...
        Tobin

        UPDATE: Oracle Client Lite is also available, but won't include some of the more useful Oracle products like (SQL*Plus). Unless you are very good at configuring TNS listeners etc., I would stick with the whole client package.
        If you truly want to debug the script as CGI, you will need to access your server error log. That is where the actual errors generated by your script will be located. However, I will reiterate my previous sentiment and that of Tobin that you need to make sure that the CGI "Hello world" script works and that you have a script that successfully connects to and can query the database. Once you have both, then you can begin to combine them.

        Sean
Re: perl remote oracle database connection
by derby (Abbot) on May 24, 2006 at 22:38 UTC

    use CGI::Carp qw(fatalsToBrowser); is going to give you a better idea of where your problems lay. Ok ... repeat after me ... read before post ... read before post ... read before post ...

    -derby