Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I know this isn't exactly what you're asking, but here's an example of using two different data sources on a single schema in the same script. The example uses SQLite and AnyData but any DBI source should work.
#!perl -w use strict; sub run { for my $source( qw(sqlite anydata)){ Schema->db_connect( $source ); Schema->db_drop_table; Schema->db_make_table; Schema->create({c1=>9,c2=>'Hi From'}); printf "%s %s!\n", Schema->retrieve(9)->c2 , Schema->db_Main->{Driver}->{Name}; } } package My::DBI; use base 'Class::DBI'; my $source_name; Ima::DBI->set_db ( 'anydata','DBI:AnyData(AutoCommit=1):','','' ); Ima::DBI->set_db ( 'sqlite' ,'DBI:SQLite:dbname=cdbi' ,'','' ); sub db_Main { my $db = 'db_'.$source_name; __PACKAGE__->$db } sub db_connect { $source_name = lc $_[1] } sub db_drop_table{ eval{ $_[0]->db_Main->do('DROP TABLE '.$_[0]->table +)} } sub db_make_table{ $_[0]->db_drop_table; $_[0]->sql_create->execute } package Schema; use base 'My::DBI'; __PACKAGE__->table ( 't1' ); __PACKAGE__->columns( All=>qw(c1 c2) ); __PACKAGE__->columns( Primary => qw(c1) ); __PACKAGE__->set_sql( 'create' => "CREATE TABLE t1 (c1 INT, c2 CHAR(9) +)"); package main; run(); __END__

In reply to Re: Dynamic DSN w/ Class::DBI Example by jZed
in thread Dynamic DSN w/ Class::DBI Example by gryphon

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found