http://qs1969.pair.com?node_id=322860

!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. :(

!unlike

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.

    -derby

      Silly me! Of course.

      I really must remember to take off my dunce hat!

      !unlike

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