Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Accessing multiple Databases with Class::DBI

by !unlike (Beadle)
on Jan 21, 2004 at 12:54 UTC ( #322860=perlquestion: print w/replies, xml ) Need Help??

!unlike has asked for the wisdom of the Perl Monks concerning the following question:

My problem is thus:

I am currnetly access a DB with DBI. Iwould like to migrate to Class::DBI. However one of my SELECT queries must query another database.

The problem is I do not have direct access to the DB. Some sample code should explain the problem:

use DBI; my $dbh = DBI->connect('dbi:mysql:database=db1' , 'user', 'password'); my $db1_data = $dbh->selectall_hashref(qq/select * from table where field = 'value'/); my $db2_data = $dbh->selectall_hashref(qq/select * from db2.table where field = 'value');

How do I achieve this with Class::DBI? My reading of the Class::DBI docs does not show if this is possible.

I agree that the correct solution would be to connect to db2 as I did to db1. However this isn't possible for "security reasons". Personally I disagree with this but it is not an argument that I will win. :(


I write my Perl code like how I like my sex: fast and dirty. ;)

Replies are listed 'Best First'.
Re: Accessing multiple Databases with Class::DBI
by derby (Abbot) on Jan 21, 2004 at 13:30 UTC
    I'd have to test this out but isn't Class::DBI usually used in a module-2-table manner? If so, you could have something like this:

    package MyModule::DBI; use base 'Class::DBI'; MyModule::DBI->set_db(...); ... package MyModule::Table; use base 'MyModule::DBI'; MyModule::Table->table('table'); ... package MyModule::DB2_Table; use base 'MyModule::DBI'; MyModule::DB2_Table->table( 'db2.table' ); # that should work depending on the security # and access restrictions .... my @db1_data = MyModule::Table->search( field => 'value' ); my @db2_data = MyModule::DB2_Table->search( field => 'value' ); ...

    But then again, it's these types of situations that make me steer away from Class::DBI.


      Silly me! Of course.

      I really must remember to take off my dunce hat!


      I write my Perl code like how I like my sex: fast and dirty. ;)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://322860]
Approved by Roger
Front-paged by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2023-06-06 16:20 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (29 votes). Check out past polls.