getting DBI and ODBC to work with PERL and MYSQL data source

Discussion in 'Perl Misc' started by Jack, Mar 22, 2007.

  1. Jack

    Jack Guest

    Hi folks,

    On windows server 2003 I installed the Mysql ODBC driver, created a
    dsn, and installed DBI and DBD::ODBC perl modules. Does anyone have
    experience issuing a SQL query through PERL to Mysql.. here is my
    program, the uncommented pieces worked great but when I remove the
    comments for the bottom 2 rows I get the error indicated:

    use DBD::ODBC;
    use strict;
    use diagnostics;
    use DBI;
    eval {
    my $dbh = DBI->connect('DBI:ODBC:mysqldsn;host=localhost;', 'root',
    's1ghtspr1ng7281',
    { 'RaiseError' => 1, 'PrintError' => 0, 'AutoCommit' => 0} );
    }; # eval
    # $sth = $dbh->prepare("SELECT testcol1 FROM temptable");
    # $sth->execute( $baz );

    ERROR:
    E:\tmp>perl odbc_test2.pl
    Global symbol "$sth" requires explicit package name at odbc_test2.pl
    line 14.
    Global symbol "$dbh" requires explicit package name at odbc_test2.pl
    line 14.
    Global symbol "$sth" requires explicit package name at odbc_test2.pl
    line 15.
    Global symbol "$baz" requires explicit package name at odbc_test2.pl
    line 15.
    Execution of odbc_test2.pl aborted due to compilation errors (#1)
    (F) You've said "use strict vars", which indicates that all
    variables
    must either be lexically scoped (using "my"), declared beforehand
    using
    "our", or explicitly qualified to say which package the global
    variable
    is in (using "::").

    Uncaught exception from user code:
    Global symbol "$sth" requires explicit package name at
    odbc_test2.pl lin
    e 14.
    Global symbol "$dbh" requires explicit package name at odbc_test2.pl
    line 14.
    Global symbol "$sth" requires explicit package name at odbc_test2.pl
    line 15.
    Global symbol "$baz" requires explicit package name at odbc_test2.pl
    line 15.
    Execution of odbc_test2.pl aborted due to compilation errors.
    at odbc_test2.pl line 16
    Jack, Mar 22, 2007
    #1
    1. Advertising

  2. Jack

    Paul Lalli Guest

    On Mar 22, 12:07 am, "Jack" <> wrote:
    > Hi folks,
    >
    > On windows server 2003 I installed the Mysql ODBC driver, created a
    > dsn, and installed DBI and DBD::ODBC perl modules. Does anyone have
    > experience issuing a SQL query through PERL to Mysql.. here is my
    > program, the uncommented pieces worked great but when I remove the
    > comments for the bottom 2 rows I get the error indicated:
    >
    > use DBD::ODBC;
    > use strict;
    > use diagnostics;
    > use DBI;
    > eval {
    > my $dbh = DBI->connect('DBI:ODBC:mysqldsn;host=localhost;', 'root',
    > 's1ghtspr1ng7281',
    > { 'RaiseError' => 1, 'PrintError' => 0, 'AutoCommit' => 0} );
    > }; # eval
    > # $sth = $dbh->prepare("SELECT testcol1 FROM temptable");
    > # $sth->execute( $baz );
    >
    > ERROR:
    > E:\tmp>perl odbc_test2.pl
    > Global symbol "$sth" requires explicit package name at odbc_test2.pl
    > line 14.


    This has nothing to do with DBI, MySQL, or anything similar. You
    declared a lexical variable in a block (the eval {}) and then tried to
    use it outside the block. You can't do that.

    Why is that in the eval to begin with? The point of eval is to catch
    fatal errors. So if the DBI->connect() call fails, you don't let Perl
    exit, but then you continue on with the program like nothing's wrong.

    Here are your options:
    1) Get rid of the eval altogether.
    1a) If you don't want the DBI->connect() failure to terminate the
    program, simply set RaiseError to 0 instead of 1.
    2) Move the declaration of $dbh outside the eval. So it would be
    something like:
    my $dbh;
    eval {
    $dbh = DBI->connect(...);
    };

    Paul Lalli
    Paul Lalli, Mar 22, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    447
  2. Paul Vudmaska

    dbi:mysql mysql has gone away

    Paul Vudmaska, Apr 21, 2004, in forum: Ruby
    Replies:
    0
    Views:
    110
    Paul Vudmaska
    Apr 21, 2004
  3. Jerome Hauss
    Replies:
    0
    Views:
    166
    Jerome Hauss
    Oct 13, 2004
  4. wana
    Replies:
    0
    Views:
    178
  5. sam
    Replies:
    2
    Views:
    264
    J. Gleixner
    Jan 18, 2005
Loading...

Share This Page