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

Hi Perlmonks, I'm trying to gte my first piece of data out of my Oracle database via the win32::ODBC connection (Sorry about the win32, but for now that's all I got). Everything seems to work fine except that I don't get any data (Which actually means that it is not working at all:). The SQL is tested directly on the base with SQL+ so no SQL syntax problems there. I think I'm testing for the connection stuff, but if any of you could give me a hint on how I can be 100% of the connection that would be a blast. Look at the code below:
use Win32::ODBC; $db = new Win32::ODBC("DSN=clamon.world;UID=scott;PWD=tiger"); if (!$db) { $Message = "The $DSB database is currently unavailable. Try again +later."; print $Message; exit(); } if ($db->Sql($Sql_test)) { print "SQL failed.\n"; print "Error: " . $db->Error() . "\n"; $db->Close(); exit; } $db->Sql("SELECT * FROM EMP where EMPNO = 7900"); $db->FetchRow(); %hash = $db->DataHash; foreach $key (sort(keys %hash)) { print $key, '=', $hash{$key}, "\n"; } $db->Close();

Replies are listed 'Best First'.
Re: Problem getting data from win32::ODBC connection to oracle
by JayBonci (Curate) on Feb 08, 2002 at 11:23 UTC
    Okay, I've never worked with Oracle (and my ODBC memory is foggy), but I'm going to take a few blind stabs in the dark here and I hope it'll be enough to get you started in the right direction.

    First off,
    use strict;
    it's a really good habit to get into. For instance, in your script, what is $Sql_test?
    Also, in the line:
    $db = new Win32::ODBC("DSN=clamon.world;UID=scott;PWD=tiger");
    The documentation I have suggests that you can connect with only the DSN name. (this is the part where the cross the "t's and dot the i's" comes in, but) Make sure that you have the proper driver installed for your DSN type, and try embedding the username and password in the DSN (most of the drivers I've seen will allow you to actually do that).

    If in doubt, you could also use ADO (assuming you are on a windows 2000 or higher machine).
    use strict; use Win32::OLE; my $db = CreateObject OLE "ADODB.Connection"; $db or die "Whoa partner!"; $db->Open("DSN=clamon.world;UID=scott;PWD=tiger"); my $rs = $db->Execute("SELECT * FROM EMP where EMPNO = 7900"); print ($_)->{Description}."<br>\n" foreach(keys %{$db->Errors()}) unle +ss($rs); while(!$rs->EOF){ #see the ADO documention for getting out the data you want $rs->MoveNext(); }
    This is of course an alternate suggestion to try if you're banging your head against this horribly. Note, I haven't tested this, so treat it as if you would any psuedocode. Good luck with it.
        --jay
      Hi, I added the use strict; but nothing out of the ordenary changed. I usually use strict. The Sql_test is now also defined(Cut/paste error as you mentioned), but that didn't do the trick either. Why must I only define the DSN name alone? I would kind of expect to use the user and password to gain access to the tabels??
Re: Problem getting data from win32::ODBC connection to oracle
by MZSanford (Curate) on Feb 08, 2002 at 11:19 UTC
    Probly just a copy/paste error, but where is $Sql_test defined ?
    from the frivolous to the serious
Re: Problem getting data from win32::ODBC connection to oracle
by Rhose (Priest) on Feb 08, 2002 at 14:19 UTC
    I know this is not the answer to the question you asked, but if you *can* use DBI/DBD:Oracle, here is some sample code which may help.

    Note: The TNS name of the database may not be the same as the DSN name (actually, when you configured the DSN entry, you placed the TNS name in the server area.)

    use strict; use DBI; #-- Define local constants use constant TRUE => 1; use constant FALSE => 0; use constant ORAUSER => 'scott'; use constant ORAPASS => 'tiger'; use constant ORATNS => 'clamon'; #-- Define local variables my $gDBHandle; my $gSQLCmd; my $gSQLHandle; my @gFields; #-- Connect to the database $gDBHandle = DBI->connect ( 'dbi:Oracle:' . ORATNS, ORAUSER, ORAPASS, { AutoCommit => FALSE, PrintError => FALSE, RaiseError => FALSE, } ) || die 'Could not connect to Oracle ['.$DBI::errstr.' - '.$DBI::er +r.']'; #-- Get the data $gSQLCmd = 'SELECT * FROM emp WHERE empno = ?'; $gSQLHandle = $gDBHandle->prepare ( $gSQLCmd ) || die 'Error with SQL statement ['.$DBI::errstr.' - '.$DBI::err.' +]'; $gSQLHandle->execute(7900) || die 'Error with SQL statement ['.$DBI::e +rrstr.' - '.$DBI::err.']'; while (@gFields = $gSQLHandle->fetchrow_array) { print 'Row: ',$gFields[0],"\t",$gFields[1],"\t",$gFields[2],"\n"; } #-- Close the database connection $gDBHandle->disconnect(); #-- Exit exit; #-- End of sample

    I hope this helps...

      Hi, Thanks for the help. As soon as I got the DBI module up and running everything went just fine after following you example. Thanks, RuneK
Re: Problem getting data from win32::ODBC connection to oracle
by djantzen (Priest) on Feb 08, 2002 at 13:25 UTC

    According to the documentation on CPAN, your use of the constructor is fine.

    I have zero experience with the this API, but from a quick perusal of the documentation, I'd suggest removing the call in void context to FetchRow(); I think it's moving the record index established by the SQL command, so that the call to DataHash() has nothing to return.

Re: Problem getting data from win32::ODBC connection to oracle
by strat (Canon) on Feb 08, 2002 at 15:33 UTC
    Maybe using the Win32::ODBC-error-Methods might help you to locate the error, e.g.
    #!perl -w use strict; use Win32::ODBC; my $dsn = "DSN=clamon.world;UID=scott;PWD=tiger"; my $statement = q(SELECT * FROM EMP where EMPNO = 7900); my $db = Win32::ODBC->new($dsn); unless ($db){ my $error = Win32::ODBC::Error; die "Error in connecting to db: $error\n"; } if ($db->Sql($statement){ my $error = $db->error(); die "Error in $statement:\n\t$error\n"; } while ($db->FetchRow){ my %line = $db->DataHash(); foreach my $key (sort keys %line){ print "$key = $hash{$key}\n"; } print "\n"; } $db->Close();

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

      Hi perl -le "s==*F=e=>y~\*martinF~stronat~=>s~^\w~~g=>chop,print", What happens is that everything still seems fine until it reaches the below section. Then it cycles through the while indefinetly, BUT skips the print line for every cycle. That is probably because the $key is empty. Now the wierd thing is why does it cycle indefinetly when there is only one record in that table that matches and why doesn's that single record get printed? I think the problem could be with the setup of the oracle river and DSN on my client, but since I haven't done that before either then it's kind of a mess. Any ideas?
      while ($db->FetchRow){ #<-Cycles indefinetly my %line = $db->DataHash(); foreach my $key (sort keys %line){ print "$key = $hash{$key}\n"; #<-Skips every time } print "\n";
Re: Problem getting data from win32::ODBC connection to oracle
by trs80 (Priest) on Feb 08, 2002 at 14:03 UTC
    Are you sure EMPNO 7900 is a valid record? That is does that record exist in the dataset? I can't tell from your statement if you tested the return value or the syntax which is why I ask.