Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Win32::ODBC Information 1

Status
Not open for further replies.

Rieekan

Programmer
Apr 2, 2001
737
US
Hello,

I'm looking for information on the Win32::ODBC module for Perl 5.x but so far have run into problems using the module to display results from a SQL Fetch command. Does anyone know of any good documentation on this module?

Thanks in advance!

- George
 
There is an ODBC FAQ at looks quite good. A relavent bit is:

How do I use Win32::ODBC?
Others have put out some examples of using Win32::ODBC and it's use as a CGI script...

&quot;Accessing ODBC Data Sources Through the Web&quot; (by Zack Steinkamp <zsteinka@polymail.cpunix.calpoly.edu>).
A full discussion of ODBC is outside the scope of this FAQ but let's outline some basics.


First you need to create a DSN (Data Source Name) which is a name that represents the database file (or connection) and ODBC driver as well as user id and password.

Second you add the following USE line to the beginning of your Perl script:

use Win32::ODBC;
Third you open a connection to your database with (note that this example checks for failure):

$DSN = &quot;My DSN&quot;;
if (!($db = new Win32::ODBC($DSN))){
print &quot;Error connecting to $DSN\n&quot;;
print &quot;Error: &quot; . Win32::ODBC::Error() . &quot;\n&quot;;
exit;
}
Fourth you execute your SQL command (NOTE: due to backward compatibility with NT::ODBC the Sql() method returns undef if it was successful and a non zero integer error number if it fails):

$SqlStatement = &quot;SELECT * FROM Foo&quot;;
if ($db->Sql($SqlStatement)){
print &quot;SQL failed.\n&quot;;
print &quot;Error: &quot; . $db->Error() . &quot;\n&quot;;
$db->Close();
exit;
}
Fifth you fetch the next row of data until there are no more left to fetch. For each row you retrieve data and process it:

while($db->FetchRow()){
undef %Data;
%Data = $db->DataHash();
...process the data...
}
Sixth you close the connection to the database:

$db->Close();
That is it! Oh, so easy!

Is that similar to what you are doing? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
I'm going to assume that if I use the standard $key and $val to extract the data from the hash that is created when running the fetch, I should be fine. Is this a correct statement?
 
I figured out what I was doing wrong. I was trying to read the %Data hash outside of the while ($db->FetchRow()) statement. That'll teach me!

Thanks for all of your help!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top