perl fetchrow_array重新返回一个空数组

发布于 2025-02-11 05:19:22 字数 530 浏览 1 评论 0原文

嘿,我编码一个需要从数据库获取数据的Perl脚本。但是,当我获得返回值时,它的阵列又回到了一个阵列,所以我将其解释。但是它仍然什么也没打印。我在sqlplus上运行了命令,它在那里毫无问题地工作。我不确定如何求解此

代码:

my $sth = $dbh->prepare("select XMLRECORD from F_COMPANY") or
            die "Couldnt prepare statement: " . $dbh->errstr;

$sth->execute();

# loop through the returned data 
while( my ($row) = $sth->fetchrow_array()){
   print "@$row\n";
}

输出:实际上没有

也尝试过这样的尝试

Hey Im coding a perl script which needs to get data from a database. But when I get a return value its comes back in an array so i dereference it. Yet it still prints out nothing. I ran the command in sqlplus and it worked there without issue. Im not sure how to solve this

Code:

my $sth = $dbh->prepare("select XMLRECORD from F_COMPANY") or
            die "Couldnt prepare statement: " . $dbh->errstr;

$sth->execute();

# loop through the returned data 
while( my ($row) = $sth->fetchrow_array()){
   print "@$row\n";
}

OUTPUT: Literally nothing

have tried like this too

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2025-02-18 05:19:22

始终使用使用严格;使用警告;。它会遇到您的错误。

fetchrow_array不返回数组引用。它将行的值返回单个标量。

您想要

while ( my $row = $sth->fetchrow_arrayref() ) {  # aka `->fetch`
   say "@$row";
}

while ( my @row = $sth->fetchrow_array() ) {
   say "@row";
}

use strict;
use warnings;
use feature qw( say );

use DBI;

my $dbh = DBI->connect(
   "dbi:SQLite:dbname=:memory:",
   "", "",
   {
      AutoCommit => 1,
      RaiseError => 1,
      PrintError => 0,
      PrintWarn  => 1,
   }
);

$dbh->do( "CREATE TEMPORARY TABLE `Table` ( `a` INT, `b` INT )" );
$dbh->do( "INSERT INTO `Table` VALUES ( 123, 456 )" );

{
   my $sth = $dbh->prepare( "SELECT * FROM `Table`" );
   $sth->execute();
   while ( my $row = $sth->fetchrow_arrayref() ) {  # aka `->fetch`
      say "@$row";
   }
}

{
   my $sth = $dbh->prepare( "SELECT * FROM `Table`" );
   $sth->execute();
   while ( my @row = $sth->fetchrow_array() ) {
      say "@row";
   }
}

输出:

123 456
123 456

ALWAYS use use strict; use warnings;. It would have caught your error.

fetchrow_array does NOT return an array reference. It returns the values of the row as individual scalars.

You want

while ( my $row = $sth->fetchrow_arrayref() ) {  # aka `->fetch`
   say "@$row";
}

or

while ( my @row = $sth->fetchrow_array() ) {
   say "@row";
}

use strict;
use warnings;
use feature qw( say );

use DBI;

my $dbh = DBI->connect(
   "dbi:SQLite:dbname=:memory:",
   "", "",
   {
      AutoCommit => 1,
      RaiseError => 1,
      PrintError => 0,
      PrintWarn  => 1,
   }
);

$dbh->do( "CREATE TEMPORARY TABLE `Table` ( `a` INT, `b` INT )" );
$dbh->do( "INSERT INTO `Table` VALUES ( 123, 456 )" );

{
   my $sth = $dbh->prepare( "SELECT * FROM `Table`" );
   $sth->execute();
   while ( my $row = $sth->fetchrow_arrayref() ) {  # aka `->fetch`
      say "@$row";
   }
}

{
   my $sth = $dbh->prepare( "SELECT * FROM `Table`" );
   $sth->execute();
   while ( my @row = $sth->fetchrow_array() ) {
      say "@row";
   }
}

Output:

123 456
123 456
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文