Reputation: 18133
my code is the following:
sub new {
my $class = shift;
my $self = bless {}, $class;
$self->initialize();
return $self;
}
sub initialize
{
my $self = shift;
$self->connect();
$self->{'dbh'}
->do("CREATE TABLE IF NOT EXISTS 'settings' (Id INTEGER PRIMARY KEY, Option TEXT, Value TEXT)");
}
sub connect
{
my $self = shift;
$self->{'dbh'} = DBI->connect($self->DSN, "", "", {
RaiseError => 1,
AutoCommit => 1
});
die "Can't connect" unless($self->{'dbh'});
}
sub no_options
{
my$self = shift;
$self->{'dbh'}->prepare("SELECT COUNT(*) as Total FROM settings");
# Here is the problem i get the error here:
# Can't locate object method "execute" via package "DBI::db"
$self->{'dbh'}->execute();
my %row = $self->{'dbh'}->fetchhash_array;
return $row{'Total'};
}
in other place i call my package
my $test = Lib::PackageName->new;
print $test->no_options;
Upvotes: 2
Views: 6554
Reputation: 74252
The prepare
method returns a statement object. It is this object that has the execute
method.
my $statement = $self->{'dbh'}->prepare("SELECT COUNT(*) as Total FROM settings");
$statement->execute();
There is no method named fetchhash_array
in DBI
. The available fetch methods for the statement object are:
Upvotes: 5
Reputation: 3498
$self->{'dbh'}->prepare(...)
returns a statement handle which you should keep for further use:
my $sth = $self->{'dbh'}->prepare("SELECT COUNT(*) as Total FROM settings");
$sth->execute;
my %row = $sth->fetchhash_array;
Upvotes: 1