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

J

Jack

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
 
P

Paul Lalli

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top