【发布时间】:2011-11-17 04:29:39
【问题描述】:
我收到此错误
如果没有包或对象引用,则无法在 ... [在 createSqlTable 行中以我的 $execute 开头] 调用方法“prepare”。
代码类似于以下(sn-ps):
use othermodule1;
use othermodule2;
my $dbh = othermodule1::connectToDatabase();
if ( defined $databaseHandler )
{
print "\ndatabaseHandler is defined here after calling connectToDatabase\n";
}
othermodule2::setDatabaseHandler( $databaseHandler );
othermodule2::createSqlTable();
其他模块1:
my $databaseHandler = unset;
sub connectToDatabase
{
$databaseHandler = DBI->connect('DBI:mysql:db','db','pwd') or die "Could not connect to database: ";
}
其他模块2:
my $dbhandler = unset;
sub setDatabaseHandler
{
$dbhandler = @_;
}
sub createSqlTable()
{
my $query = "CREATE TABLE atable ( a CHAR(30) NULL, b CHAR(30) NULL )"; # etc...
my $execute = $dbhandler ->prepare($myquery) or die "Couldn't prepare statement: " . $dbhandler->errstr;
$execute->execute or die "Couldn't execute statement: " . $dbhandler->errstr;
}
【问题讨论】:
-
顺便说一句,它是一个数据库handle(即一种识别数据库连接的方式),而不是一个handler(处理某事的东西)。
标签: perl function scope dbi prepare