我已創建包,以與數據庫連接的代碼下面給出:無法找到對象的方法「準備」通過包「DB_CONNECT」
#!/usr/bin/perl
#Class
package DB_CONNECT;
#Constructor
sub new {
my ($class)[email protected]_;
my $self={};
$self->{_odbc}="dbi:ODBC:SQLSERVER";
$self->{_username}="xxx";
$self->{_password}="xxx";
$self->{_dbh}=my $dbh;
bless $self, $class;
return $self;
}
###Method to open the database connection
sub db_connect
{
use DBI;
my ($self)[email protected]_;
$self->{_dbh}=DBI->connect($self->{_odbc},$self->{_username},$self->{_password},{PrintError => 1}) || die "Database connection not made: $DBI::errstr";
return 1;
}
1;
下面一組的Perl代碼獲取數據從數據庫。
#!/usr/bin/perl
#use strict;
use Data::Dumper;
use Asterisk::AGI;
use DB_CONNECT;
#require("/root/DB_CONNECT.pm");
my $agi = new Asterisk::AGI;
my $db = DB_CONNECT->new();
my $sql = "EXEC usp_sms_ticket '".$status_data."'";
my $sth = $db->prepare($sql);
$sth->execute();
$return = $sth->fetchrow();
$agi->set_variable('result',$return);
print Dumper($return);
$sth->finish;
$db->disconnect;
每當我執行我的Perl程序,我得到以下錯誤:
無法找到對象的方法「準備」通過包「DB_CONNECT」
你沒有方法「準備」,是嗎?而且你不是從DBI對象繼承的,你正在使用它,所以你不會這樣。爲什麼不只有一個返回DBI句柄的函數? –