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

Using the following code I'm getting "Can't connect to data source "dbi:Oracle:host=localhost;sid=nms", 'user', 'password', no database driver specified and DBI_DSN env var not set at ./test.pl line 9 "
#!/usr/local/bin/perl # Set constants use strict; use DBI; my @array = ('"dbi:Oracle:host=localhost;sid=nms"','user', 'password') +; my $dsn = "@array[0], '@array[1]', '@array[2]'"; my $dbh = DBI->connect($dsn); my $sth = $dbh->prepare("select neid from ne_table"); $sth->execute(); my $arrayref = $sth->fetchall_arrayref(); foreach my $aref ( @$arrayref ) { print @$aref,"\n"; } $sth->finish(); $dbh->disconnect;

I can't find the problem.... If I replace
 my $dbh = DBI->connect($dsn);
with
my $dbh = DBI->connect( "dbi:Oracle:host=localhost;sid=nms", 'user', ' +password');

The script runs fine. I've made a number of attempts to identify the problem but I don't see the anything wrong. Any insight would be greatly appreciated.

Replies are listed 'Best First'.
Re: no database driver specified and DBI_DSN env var not set at
by Corion (Patriarch) on Aug 08, 2007 at 14:03 UTC

    There is a difference between the strings

    '"dbi:Oracle:host=localhost;sid=nms"'
    and
    "dbi:Oracle:host=localhost;sid=nms"

    Print them both out to see it. You want the latter.

    Where did you pick up the following code to build your DSN and connect to the DB? It's wrong:

    # totally wrong code my $dsn = "@array[0], '@array[1]', '@array[2]'"; my $dbh = DBI->connect($dsn);

    You don't want to pass a single string to DBI->connect, and the string you used is so horribly wrong in many ways it makes my head hurt. Try print $dsn; to see what $dsn contains. You want the following instead:

    my $dbh = DBI->connect(@array);

    ... and you sure want a better name than @array for the connect parameters. Like, @connect_parameters, for example.

      print $dsn;

      returns
      "dbi:Oracle:host=localhost;sid=nms", 'user', 'password'
      which is the results I expected.
      As for being horribly wrong... OK? But could you explain?

        You need to learn about the difference between a list and a string that looks like a list when printed. Maybe perldata helps you.

        You need to pass a list to DBI->connect(), and not a string that looks like a list when printed. Also, the DSN is not supposed to have quotes in it.

Re: no database driver specified and DBI_DSN env var not set at
by technojosh (Priest) on Aug 08, 2007 at 16:53 UTC
    Maybe instead of:
    my $dsn = "@array[0], '@array[1]', '@array[2]'";

    try:

    my $dsn = "$array[0], '$array[1]', '$array[2]'";
    to call those array elements individually next time. I am not sure that is your final problem though...