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

Hi Monks,

In my script I have used win32::odbc. It's connecting properly but while executing the query it's giving an error saying "bad file descriptor". I am executing the query this way:

$statement = "some query"; $db->sql($statement)||die("can't execute $!\n");
any idea why I am getting this error? Thanks.

Replies are listed 'Best First'.
Re: win32::odbc error
by almut (Canon) on Dec 01, 2009 at 22:04 UTC

    As far as I can tell from a quick look at the docs and the sources, the Sql() method returns undef upon success, and an error number otherwise.  In other words, your || die ... is inappropriate here (as Sql() doesn't return true upon success).  Try and die ... instead, or maybe somewhat more readable

    my $err = $db->Sql($statement); die "..." if defined $err;

      YEP....you are absolutely correct. my die statement was die'ing on me :). Thanks.

Re: win32::odbc error
by tdane (Acolyte) on Dec 01, 2009 at 21:51 UTC
    I think we need a little more context. How are you getting your $db reference? We need to see the call where you are getting the reference stored in $db.

      Let me give you a little more. ODBC uses a "DSN" to identify the data resource to be connected to. This can be a name stored in the ODBC configuration or it can be a full ODBC connect string. There should be a line in your code that looks like this:

      $db = new Win32::ODBC("someDSN");

      or

      $db = new Win32::ODBC("Driver={SQLServer};Server=Your_Server_Name;Data +base=Your_Database_Name;Uid=Your_Username;Pwd=Your_Password;");

      And that you should check for success as well. If that call doesn't work, your $db reference won't be useful for anything.

        Hi tdane,

        my connection is successful but the query getting failed. I don't know why. If I run the smae query in sql prompt it runs fine but the same query runs through win32::odbc fails.

      like below

      if (!($db = new Win32::ODBC("DSN=Book;UID=$user;PWD=$pwd;"))) { print "error message"; } else { $statement="query"; $db->Sql($statement); <continue other stuff> }

        And is that DSN defined in Control Panel->Administrative Tools->Data Sources (ODBC)?

        If it is and you can hit the "Test Data Source" button and it works, then you have exhausted my expertise. Basically, that error message looks to me like that DSN isn't working. Be careful about user issues. Is it a User DSN, a System DSN, or a File DSN?

        If the DSN works in the control panel test, your perl (as much as I have seen) should work. If it is a user DSN is your perl code running as the same user who onns the DSN specification?