initialize(); -6ren">
gpt4 book ai didi

perl - 无法通过包 "execute"定位对象方法 "DBI::db"

转载 作者:行者123 更新时间:2023-12-02 17:48:09 25 4
gpt4 key购买 nike

我的代码如下:

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'};
}

在其他地方我称我的包裹

my $test = Lib::PackageName->new;

print $test->no_options;

最佳答案

prepare方法返回一个语句对象。正是这个对象具有 execute方法。

my $statement = $self->{'dbh'}->prepare("SELECT COUNT(*) as Total FROM settings");
$statement->execute();

DBI 中没有名为fetchash_array 的方法.语句对象的可用获取方法是:

关于perl - 无法通过包 "execute"定位对象方法 "DBI::db",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11886593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com