2011-12-26

Connecting to Microsoft Access with Perl using Win32::ODBC

Just a short example.




require 5.004;

use POSIX qw(locale_h);
setlocale(LC_ALL, "russian");

use Win32::ODBC;

$ODBCUserDSN = 'AccessODBCTest';

$db = new Win32::ODBC($ODBCUserDSN)
  || die "Error opening ODBC \"$ODBCUserDSN\"\n";

$stmt = "SELECT table2.field1, table2.field2, table1.field3
  FROM table1 INNER JOIN table2 ON table1.field1 = table2.field1;";

$rc = $db->Sql($stmt);
die qq(SQL failed "$stmt": ), $db->Error(), qq(\n) if $rc;

while ($db->FetchRow())
{
  ($A1, $A2, $A3) = $db->Data();#("field1", "field2", "field3");
  print "$A1, $A2, $A3\n";
} # while

$db->Close();

exit;