Sql Perl DBI:输出不同的表值并保存它们
我的代码如下所示:Sql Perl DBI:输出不同的表值并保存它们,sql,perl,dbi,Sql,Perl,Dbi,我的代码如下所示: use DBI; $dbh = DBI->connect( 'dbi:XYZ:ABC','ABCD', 'XXXX' ) or die "Connection Error: $DBI::errstr\n"; my $sth = $dbh->prepare('select "Tablespace" from <TABLE>') or die "Couldn't prepare statement: " . $dbh->errstr; $dbh-&
use DBI;
$dbh = DBI->connect( 'dbi:XYZ:ABC','ABCD', 'XXXX' )
or die "Connection Error: $DBI::errstr\n";
my $sth = $dbh->prepare('select "Tablespace" from <TABLE>')
or die "Couldn't prepare statement: " . $dbh->errstr;
$dbh->errstr;
$sth->execute
or die "Can't execute SQL statement: $DBI::errstr\n";
my @schemaname;
my @schematable;
while (@schemaname = $sth->fetchrow_array()){
print "SchemaName is: @schemaname\n";
$schematable[0][0]= $schemaname[0];
$schematable[1][0]= $schemaname[1];
$schematable[2][0]= $schemaname[2];
$schematable[3][0]= $schemaname[3];
$schematable[4][0]= $schemaname[4];
}
warn "Data fetching terminated early by error: $DBI::errstr\n"
if $DBI::err;
print($schematable[0][0]);
print($schematable[1][0]);
print($schematable[2][0]);
print($schematable[3][0]);
print($schematable[4][0]);
$sth->finish();
$dbh->disconnect;
断开与数据库的连接
在上述两种情况下,除了最后一个变量存储在schematable[I][j]中之外,我看不到任何东西。有没有任何明显的迹象表明我做错了?关于你第一次发布的帖子,我发现两件事对我来说非常突出:
my $sth = $dbh->prepare("SELECT "Tablespace" FROM <TABLE>")
您在SQL查询(“表空间”)中只请求一个变量,因此将$sth->fetchrow_array()的输出分配给@schemaname将导致@schemaname仅在$schemaname[0]中定义
当您运行第一个代码块时,您的输出到底是什么样子的
如果要将所有返回值保存到数组中,请尝试以下操作
my $i=0;
while(my ($schema_name) = $sth->fetchrow_array()) {
$schematable[$i++][0] = $schema_name;
}
#blah blah blah
$i=0;
while(my ($used_schema_space) = $ssth->fetchrow_array()) {
$schematable[$i++][1] = $used_schema_space;
}
我不确定您为什么要在最后打印$schematable[2][2],因为您从未在脚本的早期定义过它
我希望这能有所帮助……我无法100%确定您正在尝试使用什么样的数据结构,或者您的目标是实现什么。嘿,伊恩,谢谢您指出这一点。为了回答您的问题并正确地看待问题,我想输出我在数组中提到的SQL查询的两列,然后根据存储的值生成警报(电子邮件)。此外,出于测试目的,我试图打印它,以查看是否捕获了值。。
my $sth = $dbh->prepare("SELECT "Tablespace" FROM <TABLE>")
while (@schemaname = $sth->fetchrow_array()){
my $i=0;
while(my ($schema_name) = $sth->fetchrow_array()) {
$schematable[$i++][0] = $schema_name;
}
#blah blah blah
$i=0;
while(my ($used_schema_space) = $ssth->fetchrow_array()) {
$schematable[$i++][1] = $used_schema_space;
}