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

Hello monks - much wisdom is needed here! I am charged with reading data from several MS databases (Access and SQLServer) using Perl. I'm using Win32::ODBC and thought I was following the documentation carefully, but I get useless data using the DataHash method. It looks like this:

6/8('server_name')

Here is the code:
use Win32::ODBC; $login = "DSN=ODBCTest"; $sql_text = "SELECT * from tblServer"; $conn = new Win32::ODBC($login) or die "Error Connecting! " . Win32::ODBC::Error(); if (! $conn->Sql($sql_text)) { while ($conn->FetchRow()) { my $data = $conn->DataHash(); print "$data('server_name')\n"; } } $conn->Close();
Supposedly, using DataHash enables you to specify the column name. In this case the column name is 'server_name' but perl treats it like a string with or without quotes. You can use the Data method, but it returns the rows as one big string which is impossible to parse correctly.

Any ideas? Thanks.

Replies are listed 'Best First'.
Re: Data returned from Win32::ODBC
by snax (Hermit) on Oct 15, 2003 at 20:35 UTC
    You've got this:
    print "$data('server_name')\n";
    but you want this:
    print "$data{'server_name'}\n";
    (you can leave off the single quotes, too, if you'd like).

    Can you see the difference? Sometimes those curly brackets don't always look like curly brackets :)

Re: Data returned from Win32::ODBC
by Solo (Deacon) on Oct 15, 2003 at 20:39 UTC
    You need to use %data instead of $data, since the function returns a hash and not a reference to one. And fix the hash index syntax, as pointed out above.

    ... while ($conn->FetchRow()) { my %data = $conn->DataHash(); print "$data{'server_name'}\n"; ...

    --Solo
    --
    Not a bad bit of rescuing, huh? You know, sometimes I even amaze myself.

Re: Data returned from Win32::ODBC
by rdwaters (Initiate) on Oct 16, 2003 at 13:14 UTC
    Can you see the difference? Sometimes those curly brackets don't always look like curly brackets :)

    Do you think I can sue my optician over these new glasses? :)

    Thank you snax and Solo -- all is working great now.