ODBC from Perl

So we've established that the ODBC standard is a rather good thing, but how can you use it?

To use ODBC from Perl, there are only two practical options: the Win32::ODBC module and the DBI with the DBD::ODBC module. We'll describe DBD::ODBC first and then take a deeper look at Win32::ODBC.

DBD::ODBC

The DBD::ODBC module was written by Tim Bunce and Jeff Urlwin, based on original code by Thomas K. Wenrich. It's a Perl extension written in C and is not tied to Microsoft Win32 platforms. That makes it a good option for directly using ODBC on Unix, VMS, and other non-Windows systems.

Being a DBI driver, the main goal of the DBD::ODBC module is to implement the functionality required by the DBI, not simply to give access to ODBC from Perl.

The DBD::ODBC driver is described in more detail in Appendix B, "Driver and Database Characteristics ".

Win32::ODBC

The Win32::ODBC module was written by Dave Roth, based on original code by Dan DeMaggio. It's a Perl extension written in C++ and is closely associated with the Win32 platform.

The main goal of the Win32::ODBC module is to provide direct access to the ODBC functions. From that point of view, Win32::ODBC provides a fairly thin, low-level interface.

Here's a sample of Win32::ODBC code:

use Win32::ODBC; ### Connect to a data source $db = new Win32::ODBC("DSN=MyDataDSN;UID=me;PWD=secret") or die Win32::ODBC::Error(); ### Prepare and Execute a statement if ($db->Sql("SELECT item, price FROM table")) {
 print "SQL Error: " . $db->Error() . "\n"; $db->Close(); exit;
}
### Fetch row from data source while ($db->FetchRow) {
 my ($item, $price) = $db->Data(); ### Get data values from the row print "Item $item = $price\n";
}
### Disconnect $db->Close();

The most significant disadvantages of Win32::ODBC compared to DBD::ODBC are:

There are plans to address some of these disadvantages in a later release. The most significant advantages of Win32::ODBC compared to DBD::ODBC are: