Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: DBI:how to get name of the db ?

by erix (Vicar)
on Dec 14, 2011 at 10:41 UTC ( #943522=note: print w/ replies, xml ) Need Help??


in reply to Re: DBI:how to get name of the db ?
in thread DBI:how to get name of the db ?

It turns out that $dbh->{'Name'} is not a reliable source for the database name; DBI docs say:

Holds the "name" of the database. Usually (and recommended to be) the same as the "dbi:DriverName:..." string used to connect to the database, but with the leading "dbi:DriverName:" removed.

DBD::Pg complies wth that description: it simply returns the connectionstring minus the prefix.

So it would seem that if $dbh->{'Name'} returns the database name it's by accident.


Comment on Re^2: DBI:how to get name of the db ?
Replies are listed 'Best First'.
Re^3: DBI:how to get name of the db ?
by Tux (Monsignor) on Dec 14, 2011 at 11:37 UTC

    In other words, it holds the part after the driver indication in the DSN part. When e.g. connecting to CSV like

    my $dbh = DBI->connect ("dbi:CSV:f_ext=.csv/r;f_encoding=utf-8", undef +, undef, { RaiseError => 1 });

    The return values for $dbh->{Name} will be f_ext=.csv/r;f_encoding=utf-8, which might not be very useful information.

    The fact the DBD::Pg in your case returns the correct database name, might be because you initiated the connection using something like

    my $dbh = DBI->connect ("dbi:Pg:dbname=test", ...);

    but when you connect using just "dbi:Pg:" in combination with the environment variable $PGDATABASE, you might not get anything useful at all

    $ env PGDATABASE=merijn perl -MDBI -wE'say DBI->connect("dbi:Pg:")->{N +ame}||"not known"' not known $

    Enjoy, Have FUN! H.Merijn
      The fact the DBD::Pg in your case returns the correct database name,

      We are in complete agreement. You just didn't read my post well ;-) I log 'passwordless' into postgres with PGPASSFILE and friends (PGHOST, PGPORT, PGDATABASE), so my $dbh->{'Name'} returns empty.

      (and btw, your example ("dbi:Pg:dbname=test", ...) will return not 'test', but 'dbname=test', although I suppose you understand that. (Apparently DBD::Pg was actually changed a while ago to give this DBI-compliant behavior, instead of the database name.))

Re^3: DBI:how to get name of the db ?
by TJPride (Pilgrim) on Dec 14, 2011 at 17:07 UTC
    All I know is it works for me. Maybe I got lucky and this isn't the "proper" way to do it, but if it ain't broke, don't fix it...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://943522]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2015-07-30 03:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls