Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2003
    Posts
    2

    Unanswered: dbi->connect problem

    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.

  2. #2
    Join Date
    Apr 2003
    Posts
    191

    Re: dbi->connect problem

    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.

  3. #3
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697

    Re: dbi->connect problem

    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:

    Code:
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •