use Win32::ODBC;Then you will need to create a data connection to your DSN:
$Data = new Win32::ODBC("MyDSN");You shoud check to see if $Data is indeed defined otherwise there has been an error. You can now send SQL queries and retrieve info to your heart's content! See the description of functions below and also test.pl to see how it all works.
Make sure that you close your connection when you are finished:
$Data->Close();
use Win32::ODBC;somewhere before the method calls, and that you have an ODBC object called $db which was created using some call similar to:
$db = new Win32::ODBC("MyDSN");See new for more information.
Also, in an effort to keep the examples short, no error checking is done on return values for any calls other than the one being exemplified. You should always check for error conditions in production code.
WARNING: The example code has not yet been tested. This will be fixed ASAP, but be forwarned!
Example: ($qualifier, $owner, $name, $type) = $db->Catalog("", "", "%", "'TABLE'");
Example: $cnum = $db->Connection;
Example: $db->Close();
Example: $db->Sql("SELECT f1, f2, f3 FROM foo"); $db->FetchRow(); ($f1, $f2) = $db->Data("f1", "f2"); or $db->Sql("SELECT * FROM foo"); $db->FetchRow(); @values = $db->Data;See also: DataHash
Example: $db->Sql("SELECT f1, f2, f3 FROM foo"); $db->FetchRow(); %hash = $db->DataHash("f1", "f2"); print $hash{f1}; or $db->Sql("SELECT * FROM foo"); $db->FetchRow(); %hash = $db->DataHash; foreach $key (sort(keys %hash)) { print $key, '=', $hash{$key}, "\n"; }See also: Data
$ArrayName{'DSN'} = Remarkwhere DSN is the Data Source Name and Remark is, well, the remark.
Example: %rem = $db->DataSources; print LOG qq(Current DSN's Remark: "), %rem{$db->GetDSN}, qq("\n);
$ArrayName{'DRIVER'} = Attrib1;Attrib2;Attrib3;...where DRIVER is the ODBC Driver Name and AttribX are the driver-defined attributes.
Example: %attrib = $db->Drivers; print LOG qq($driver: $attrib{$driver}\n) foreach $driver (keys %attrib);
Example: $db = new Win32::ODBC("My DSN"); if (undef $db){ Win32::ODBC::DumpError(); } if ($db->Sql("Select * FROM foo")){ $db->DumpError; }
Example: $db->Sql("Select * FROM foo"); $db->DumpData;
Example: die $db->Error(), qq(\n); ($ErrNum, $ErrText, $ErrConn) = $db->Error();
Example: $db->Sql("SELECT * FROM foo"); $db->FetchRow() || die qq(Fetch error: ), $db->Error(), qq(\n); $f1 = $db->Data("f1");See also: Sql, Data, DataHash
Example: $db->Sql("SELECT * FROM foo"); $db->FetchRow(); foreach $fd ($db->FieldNames()) print qq($fd: "), $db->Data($fd), qq("\n);
Example: @cnums = $db->GetConnections;
Example: print LOG qq(Current connection: "), $db->GetDSN, qq("\n);
Example: $max = $db->GetMaxBufSize; $db->SetMaxBufSize($needed) if ($max < $needed);See also: SetMaxBufSize
Example: $oldct = $db->GetStmtCloseType; $db->SetStmtCloseType(SQL_DROP); ... $db->SetStmtCloseType($oldct);See also: SetStmtCloseType
Example: $db->Sql("SELECT * FROM foo\n SELECT * FROM bar"); $db->FetchRow() || die qq(Fetch error: ), $db->Error(), qq(\n); $f1 = $db->Data("f1"); $db->MoreResults() || die qq(Error checking for more result sets: ), $db->Error(), qq(\n); $db->FetchRow() || die qq(Fetch error: ), $db->Error(), qq(\n); $f1 = $db->Data("f1");See also: Sql, Data
Example: $DSN = "MyDSN"; $db = new Win32::ODBC($DSN); die qq(Cannot open new ODBC\n) if ! $db; or $db = new Win32::ODBC("dsn=FOO;UID=BAR;PWD=FUBAR"); die qq(Cannot open new ODBC\n) if ! $db;
Example: $db->Sql("SELECT * FROM foo"); print DBG q(# of records: ), $db->RowCount(), qq(\n);
Example: $db->Run("SELECT * FROM foo");See also: Sql
Example: $newsize = 20480; $rc = $db->SetMaxBufSize($newsize); die qq(SetMaxBufSize($newsize) error: ), $db->Error, qq(\n) if ! $rc;See also: GetMaxBufSize
Example: $oldct = $db->GetStmtCloseType; $db->SetStmtCloseType(SQL_DROP); ... $db->SetStmtCloseType($oldct);See also: GetStmtCloseType
Example: $db->Shutdown;See also: Close
Example: $stmt = "SELECT * FROM foo"; $rc = $db->Sql($stmt); die qq(SQL failed "$stmt": ), $db->Error(), qq(\n) if $rc;See also: Error
Example: @tables = $db->TableList;See also: Catalog
Copyright © Dave Roth and Joseph L. Casadonte Jr. 1996. All rights reserved. Win32::ODBC - Object / 22 Jul 1996 / joc@netaxs.com