Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

DBI driver name from database handle

by liz (Monsignor)
on Oct 20, 2003 at 14:42 UTC ( #300599=perlquestion: print w/replies, xml ) Need Help??
liz has asked for the wisdom of the Perl Monks concerning the following question:

Is there an official way to obtain the driver name from a DBI database handle (object of type "DBI::db")?

Suppose I have a class method in an external module:

package External; sub dbh { $External::dbh ||= DBI->connect( 'DBI:mysql:test','root','' +) } #dbh

Now, after I obtain the database handle, I want to know which subclass of my own module I would need to bless an object with. Ideally:

package Foo; sub new { my $dbh = External->dbh; # how we get the $dbh is not the question + here my $driver = $dbh->SOMETHING; # now set to 'mysql', but what is "S +OMETHING" ? bless {},__PACKAGE__.'::'.$driver; } sub general { same for all database drivers }

and of course, I would then have a module Foo::mysql:

package Foo::mysql; @ISA = qw(Foo); sub specific { stuff specific to MySQL }

I can't find any easy way to do this, apart from possibly trying to do a query of some sort and inspect the class of the statement handle. If that would to be the way to do it, what would be an SQL statement that would work with all possible database drivers?


Thanks to 3dan and Abigail-II:

$dbh->{Driver}{Name}; # gives "mysql" $dbh->get_info( 17 ); # gives "MySQL"

The appropriate doc (which is indeed in DBI, but which I didn't find):

"Driver" (handle)
Holds the handle of the parent driver. The only recommended use for this is to find the name of the driver using:

"Name" (string)
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.

Replies are listed 'Best First'.
Re: DBI driver name from database handle
by edan (Curate) on Oct 20, 2003 at 15:00 UTC



Re: DBI driver name from database handle
by Abigail-II (Bishop) on Oct 20, 2003 at 15:02 UTC
    $dbh -> {Name}
    provide the information you are after? If not, does
    $dbh -> get_info (17)


      $dbh->{Name} gives me the name of the database, rather than the database driver.

      3dan's way seems to be the correct one $dbh->{Driver}{Name}.

      Of course, when you try to do a each on the $dbh object, you don't get a listing of the possible values. Too bad.


      $dbh -> {Name}
      provide the information you are after?


      $dbh -> {Name} provides the name of the database used in the connection string.

      From DBI docs

      ""Name"" (string) Holds the "name" of the database. Usually (and recom­ mended to be) the same as the ""dbi:DriverName:..."" string used to connect to the database, but with the leading ""dbi:DriverName:"" removed.
Re: DBI driver name from database handle
by Anonymous Monk on Mar 05, 2009 at 11:28 UTC
    A programmer needs a specific driver to connect to a specific database, the following table list some of the popular drivers. Driver RDMS oracle.jdbc.driver.OracleDriver ORACLE com.mysql.jdbc.Driver MySQL com.sybase.jdbc.SybDriver Sybase SQL Server DB2 org.hsqldbJdbcDriver HSQL DB Thanks Samit Katiyar

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://300599]
Approved by Corion
[choroba]: I'm definitely not obsessed with whitespace, I rate myself among Perl people, and I haven't noticed any hounding
[marto]: "no it's not that"...."weirdos "...
[marto]: List EXE_FILES installed by CPAN so a couple of people suggest that your code looks obfuscated. I'd have to ageree, from the perspective of those who can't follow all of that one liner, it doesn't read well
Veltro is a weirdo, obsessed with whitespace
[marto]: to use the word "obsession" when so few people have said so little about it is grasping at staws
marto wishes tye was around, he's so much better at this sort of thing
usemodperl likes tye!
usemodperl tye  too
[choroba]: Re^3: LiBXML: New markup while preserving earlier tags? would benefit from a couple of test cases
usemodperl meant tye 

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2018-06-24 16:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.