Several databases from the same script
[prev]
[thread]
[next]
[Date index for 2004/06/02]
In the application I am writing now there are several SQLite databases
that have identical schema but should be kept separately.
I needed to access them from the same script basically at the same time.
The best solution I could come up so far is the following.
Any corrections, ideas are welcome.
Gabor
ps. I could not log in the web site even after receiving the password
by e-mail. It kept saying it cannot set cookie and I should configure
my Navigator to enable cookies. I am using Opera and they are enabled.
package My::DBI;
use base 'Class::DBI';
use DBI;
my %dbh;
my $current_dbh;
sub myinit {
my ($dsn) = @_;
if ($dbh{$dsn}) {
$current_dbh = $dbh{$dsn};
} else {
$current_dbh = $dbh{$dsn} = DBI->connect_cached($dsn, $username, $password,
{
# defaults of Class::DBI
RaiseError => 1,
AutoCommit => 0,
PrintError => 0,
Taint => 1,
RootClass => "DBIx::ContextualFetch",
});
if (not $current_dbh) {
warn "Could not connect to '$dsn' $DBI::errstr";
return 0;
}
}
return 1;
}
sub db_Main {
my $self = shift;
return $current_dbh;
}
1;
# and in the nearby script:
My::DBI::myinit($dsn_a);
My::DBI->create(....)
My::DBI::myinit($dsn_b);
My::DBI->create(....)
|
Several databases from the same script
Gabor Szabo 23:24 on 02 Jun 2004
|