unable to store data in DBI object

Discussion in 'Perl Misc' started by Jon, Apr 17, 2006.

  1. Jon

    Jon Guest

    Hiya,

    I'm working on adding table prefixes for an application, so that it can be
    used with the same database but different sets of data. I thought the best
    idea would be to store the prefix as part of the DBI object (eg:
    $dbh->{'_prefix'} = $prefix;), however this isn't working.

    Here is some code demonstrating the problem.

    penguin@restless:~/scripts$ cat sqltest.pl
    #!/usr/bin/perl

    use lib './lib';

    use Data::Dumper;
    use sql;
    use strict;
    use warnings;

    my $sql = new sql(
    'database' => 'test',
    'username' => 'username',
    'password' => 'password',
    'dbprefix' => 'anything',
    );

    print Dumper($sql);
    penguin@restless:~/scripts$ cat lib/sql.pm
    package sql;

    use DBI;
    use strict;
    use warnings;

    @sql::ISA = qw/DBI::db/;

    sub new($%) {
    my $class = shift;
    my %opts = @_;

    # Make sure we have all required arguments
    foreach (qw/username database dbprefix/) {
    die("No $_ supplied") unless $opts{$_};
    }

    # Create a database handle
    my $dbh = DBI->connect("DBI:mysql:$opts{database}", $opts{username},
    $opts{password});
    || die("Connection to database failed: ".$DBI::errstr);


    # Record our table prefix in our DBI object
    $dbh->{'_prefix'} = $opts{'dbprefix'};

    return bless $dbh, $class;
    }

    1;
    penguin@restless:~/scripts$ ./sqltest.pl
    $VAR1 = bless( {}, 'sql' );
    penguin@restless:~/scripts$

    Thanks,
    --
    Jon
     
    Jon, Apr 17, 2006
    #1
    1. Advertising

  2. Jon wrote:

    > I'm working on adding table prefixes for an application, so that it can be
    > used with the same database but different sets of data. I thought the best
    > idea would be to store the prefix as part of the DBI object (eg:
    > $dbh->{'_prefix'} = $prefix;), however this isn't working.


    In general when you subclass a class in Perl you can't just put the
    subclasses' instance data anywhere you like. You have to put it
    somewhere where the parent class doesn't mind you putting it.

    For details of where DBI::db doesn't mind you putting stuff see the
    section of the DBI documentation section "Subclassing the DBI" and in
    particular the bit where it says "You can stash private data into DBI
    handles.."

    > package sql;


    You should probably use longer package names and ones that don't start
    with lowercase.
     
    Brian McCauley, Apr 17, 2006
    #2
    1. Advertising

  3. Jon

    Jon Guest

    "Brian McCauley" <> wrote in message
    news:...
    > In general when you subclass a class in Perl you can't just put the
    > subclasses' instance data anywhere you like. You have to put it
    > somewhere where the parent class doesn't mind you putting it.
    >
    > For details of where DBI::db doesn't mind you putting stuff see the
    > section of the DBI documentation section "Subclassing the DBI" and in
    > particular the bit where it says "You can stash private data into DBI
    > handles.."


    Thank you very much for your help. I'll have more of a read into the
    "Subclassing DBI" stuff when I get time.

    >> package sql;

    >
    > You should probably use longer package names and ones that don't start
    > with lowercase.


    Yes and I normally do but got lazy when writing up the experiment. Thanks
    again.
     
    Jon, Apr 17, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ulloa
    Replies:
    1
    Views:
    546
    Juha Laiho
    Jul 22, 2004
  2. =?Utf-8?B?UnVkeQ==?=

    to store or not to store an image

    =?Utf-8?B?UnVkeQ==?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    671
    =?Utf-8?B?UnVkeQ==?=
    Mar 30, 2005
  3. Jerome Hauss
    Replies:
    0
    Views:
    185
    Jerome Hauss
    Oct 13, 2004
  4. Asby

    Mason, DBI, and DBI::Pg

    Asby, Jul 24, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    193
  5. Tim Haynes
    Replies:
    3
    Views:
    156
    Ron Reidy
    Sep 13, 2003
Loading...

Share This Page