Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Greetings fellow monks,

I've been trying unsuccessfully to play with Class::DBI using a dynamic DSN passed to my class at runtime. I've been Googling for hours, and I haven't come up with much to show for it. Does anyone have some example code they could share for how to get this done?

This is basically what I'd like to accomplish, but it doesn't work. (Please pardon my lack of POOP knowledge/experience.)

package Stuff; use base 'Class::DBI::mysql'; sub import { my $class = shift; unless (__PACKAGE__->can('db_Main')) { __PACKAGE__->set_db('Main', @_); } } package Stuff::Item; use base 'Stuff'; __PACKAGE__->set_up_table('item'); package main; use Stuff ('dbi:mysql:dbname', 'gryphon', 'password'); my $item = Stuff::Item->retrieve(1); print $item->name, "\n";

Why do I even want to do this? Well, I have multiple databases all with the same schema. I'd like to have only one Class::DBI module to avoid replicating code. And I'd like to pass in the database name, username, and password from an INI file since that's where just about all other configuration data is stored.

Now, I've tried the following:

use Class::DBI::AutoLoader ( dsn => 'dbi:mysql:dbname', username => 'gryphon', password => 'password', tables => ['item'], namespace => 'Stuff', use_base => 'Class::DBI::mysql' ); my $item = Stuff::Item->retrieve(1); print $item->name, "\n";

...and of course this works well, but it will only work for very simple models. I won't be able to edit the automatically created classes, so it doesn't work for more complicated cases such as where I'm joining together several tables or want to muck around with output or whatever.

Sorry if this is a stupid question. I've been reading docs all day and haven't been able to figure this out.

code('Perl') || die;

In reply to Dynamic DSN w/ Class::DBI Example by gryphon

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (3)
    As of 2021-03-02 07:11 GMT
    Find Nodes?
      Voting Booth?
      My favorite kind of desktop background is:

      Results (39 votes). Check out past polls.