Perl dbi (select) 获取数组中的空字符串
Perl dbi (select) fetches empy strings in array
我有一个 perl 脚本,它连接到 postgres 数据库并获取它在其上运行的几个 select 语句。此输出的目的是在字后字中打印。当我构建我的项目时,我没有注意到获取部分发生了什么,在我用 word 打印它之后我注意到了一些奇怪的事情。
我执行了一个 select 语句并在 arrayref 中获取它,但是当我打印这个 arrayreferentie 时,我的数组中不仅有值,还有空字符串,de cli 给了我一些错误代码它。我首先虽然只是在我读完一行之后就咬一口,这样它就会转到下一行,但这没有用。有没有人已经遇到过这个问题?
这是我的代码:
my $query = $_[0]; #select multiple columns
my $SQL = $dbh->prepare($query);
$SQL -> execute();
my $headers = $SQL->{NAME};
my $databases = $SQL->fetchall_arrayref();
#testing fase , print the fetch to see what would be print in word
foreach my $row (@$databases)
{
my $databaseColumnNumber =0;
while ($databaseColumnNumber <= @$databases)
{
print "\n" , $row -> [$databaseColumnNumber];
$databaseColumnNumber++;
}
#chomp $row; ### this was a testing line to see if it would chomp after the empty value
}
$SQL->finish();
我的CLI中的打印输出如下
postgres
10
7856 kB
//emptyline(which represents the empty value in the array
Use of uninitialized value in print atC:\Users\xxxxx
//emptyline(which represents the empty value in the array
Use of uninitialized value in print at
Use of uninitialized value in print atC:\Users\xxxxx
//newline from the print to go to the new line in the array
test
10
7529 kB
//emptyline(which represents the empty value in the array
Use of uninitialized value in print atC:\Users\xxxxx
//emptyline(which represents the empty value in the array
Use of uninitialized value in print at
Use of uninitialized value in print atC:\Users\xxxxx
[编辑]
$VAR1 = [
[
'postgres',
'10',
'7856 kB'
],
[
'test',
'10',
'7529 kB'
],
[
'template1',
'10',
'6865 kB'
],
[
'template0',
'10',
'6865 kB'
]
];
$VAR1 = [
[
'city',
'408 kB',
'152 kB'
],
[
'countrylanguage',
'136 kB',
'88 kB'
],
[
'country',
'96 kB',
'56 kB'
]
];
我找到了我自己的问题的解决方案,对于未来的读者:
我的 2 个循环没有正确完成(我使用了 foreach 一段时间)下面的代码完成了工作。
foreach my $row (@$databases)
{
my $databaseColumnNumber =0;
foreach my $val (@$row)
{
print "\n" , $val;
}
}
我有一个 perl 脚本,它连接到 postgres 数据库并获取它在其上运行的几个 select 语句。此输出的目的是在字后字中打印。当我构建我的项目时,我没有注意到获取部分发生了什么,在我用 word 打印它之后我注意到了一些奇怪的事情。
我执行了一个 select 语句并在 arrayref 中获取它,但是当我打印这个 arrayreferentie 时,我的数组中不仅有值,还有空字符串,de cli 给了我一些错误代码它。我首先虽然只是在我读完一行之后就咬一口,这样它就会转到下一行,但这没有用。有没有人已经遇到过这个问题?
这是我的代码:
my $query = $_[0]; #select multiple columns
my $SQL = $dbh->prepare($query);
$SQL -> execute();
my $headers = $SQL->{NAME};
my $databases = $SQL->fetchall_arrayref();
#testing fase , print the fetch to see what would be print in word
foreach my $row (@$databases)
{
my $databaseColumnNumber =0;
while ($databaseColumnNumber <= @$databases)
{
print "\n" , $row -> [$databaseColumnNumber];
$databaseColumnNumber++;
}
#chomp $row; ### this was a testing line to see if it would chomp after the empty value
}
$SQL->finish();
我的CLI中的打印输出如下
postgres
10
7856 kB
//emptyline(which represents the empty value in the array
Use of uninitialized value in print atC:\Users\xxxxx
//emptyline(which represents the empty value in the array
Use of uninitialized value in print at
Use of uninitialized value in print atC:\Users\xxxxx
//newline from the print to go to the new line in the array
test
10
7529 kB
//emptyline(which represents the empty value in the array
Use of uninitialized value in print atC:\Users\xxxxx
//emptyline(which represents the empty value in the array
Use of uninitialized value in print at
Use of uninitialized value in print atC:\Users\xxxxx
[编辑]
$VAR1 = [
[
'postgres',
'10',
'7856 kB'
],
[
'test',
'10',
'7529 kB'
],
[
'template1',
'10',
'6865 kB'
],
[
'template0',
'10',
'6865 kB'
]
];
$VAR1 = [
[
'city',
'408 kB',
'152 kB'
],
[
'countrylanguage',
'136 kB',
'88 kB'
],
[
'country',
'96 kB',
'56 kB'
]
];
我找到了我自己的问题的解决方案,对于未来的读者:
我的 2 个循环没有正确完成(我使用了 foreach 一段时间)下面的代码完成了工作。
foreach my $row (@$databases)
{
my $databaseColumnNumber =0;
foreach my $val (@$row)
{
print "\n" , $val;
}
}