PDA

View Full Version : dbi->connect problem


Jaguars
04-27-03, 01:59
Hi, I am getting this error message:

Tk::Error: Can't locate object method "connect" via package "DBI" at win.pl line 7.

Here is my subroutine:

sub create_db()
{
$database="names";
$hostname="localhost";
$driver="mysql";
use DBI;
my $dsn="DBI:mysql:database=$database;host=$hostname";
$dbh=DBI->connect($dsn);
$drh=DBI->install_driver($driver);
@databases=DBI->data_sources($driver);

if($c)
{
my $sth=$c->prepare('select * from people');
if($sth->execute)
{
while(@row=$sth->fetchrow_array)
{
foreach(@row) {
print "$_ -";
}
print "\n";
}
}
$sth->finish;
}
return $c;
}

DBI and DBD-mysql are both installed...does anyone know what could be the problem? I looked through many such complaints and explanations online..but nothing has solved my problem yet.

THanks.

jsander
04-30-03, 07:13
Hi,

did you check that DBI.pm is in your library path (e.g. perl -V, does @INC cover the path?).

If this leads to nothing, please move the "use DBI" statement into main and see if this helps (I don't expect this to be the issue, though).

Johann

Originally posted by Jaguars
Hi, I am getting this error message:

Tk::Error: Can't locate object method "connect" via package "DBI" at win.pl line 7.

Here is my subroutine:

sub create_db()
{
$database="names";
$hostname="localhost";
$driver="mysql";
use DBI;
my $dsn="DBI:mysql:database=$database;host=$hostname";
$dbh=DBI->connect($dsn);
$drh=DBI->install_driver($driver);
@databases=DBI->data_sources($driver);

if($c)
{
my $sth=$c->prepare('select * from people');
if($sth->execute)
{
while(@row=$sth->fetchrow_array)
{
foreach(@row) {
print "$_ -";
}
print "\n";
}
}
$sth->finish;
}
return $c;
}

DBI and DBD-mysql are both installed...does anyone know what could be the problem? I looked through many such complaints and explanations online..but nothing has solved my problem yet.

THanks.

sco08y
05-24-03, 23:22
Originally posted by jsander
Hi,

did you check that DBI.pm is in your library path (e.g. perl -V, does @INC cover the path?).

If this leads to nothing, please move the "use DBI" statement into main and see if this helps (I don't expect this to be the issue, though).

Johann

If DBI.pm weren't in the INC path, the use statement would fail. I think you're right, though, that it should be at the top of the file rather than in a function.

Try:

perl -MDBI -we 'print join ",", DBI->available_drivers'

If you can get that working, you should be able to make some headway getting the connect method to work.

You might also want to do a sanity check: Open DBI.pm and make sure it's a valid perl file.