alexbio has asked for the wisdom of the Perl Monks concerning the following question:
Hi everybody. I'm using Class::DBI inside a project to access an SQLite database. It works very well, but I'm facing a problem: the path to the database file (the db name) have to be harcoded inside the package.
I have the following packages
The DBI classes
package MyModule::DBI; use base Class::DBI; use warnings; use strict; our $VERSION = '0.01'; my $dsn = "dbi:SQLite:dbname=/path/to/db"; __PACKAGE__ -> set_db('Main', $dsn); 1;
And
package MyModule::Data; use base MyModule::DBI; use warnings; use strict; our $VERSION = '0.01'; __PACKAGE__ -> table('data'); __PACKAGE__ -> columns(All => qw(column1 column2 column3)); 1;
And the main module
package MyModule; use MyModule::Data; use warnings; use strict; our $VERSION = '0.01'; sub new { my ($class, %args) = @_; my $self = bless({%args}, $class); return $self; } 1;
It could be obvious but my question is, is it possible to call the new method and pass the path to the database and use it in MyModule::DBI? If yes, how?
What I want to do is something like
use MyModule; my $obj = MyModule -> new(db_path => '/path/to/file');
Thank you
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Class::DBI and database name
by NetWallah (Canon) on Jun 27, 2010 at 22:57 UTC | |
by alexbio (Monk) on Jun 28, 2010 at 13:23 UTC |