Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > unable to store data in DBI object

Reply
Thread Tools

unable to store data in DBI object

 
 
Jon
Guest
Posts: n/a
 
      04-17-2006
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:umper;
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


 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      04-17-2006

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.

 
Reply With Quote
 
 
 
 
Jon
Guest
Posts: n/a
 
      04-17-2006
"Brian McCauley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to store or not to store an image =?Utf-8?B?UnVkeQ==?= ASP .Net 6 03-30-2005 05:51 AM
DBI Mysql storing DBI:binary, trouble with character 26 Jerome Hauss Ruby 0 10-13-2004 03:04 PM
DBI and DBI::Oracle packages configuration ulloa Perl 1 07-22-2004 05:52 PM
DBI problem : How can I load quickly one huge table with DBI ??. Tim Haynes Perl Misc 3 09-13-2003 03:43 AM
Mason, DBI, and DBI::Pg Asby Perl Misc 0 07-24-2003 09:04 PM



Advertisments