Perl dbi (select) fetches empy strings in array

2019-07-24 03:09发布

I have a perl script that connect to a postgres db and fetches a couple select statements that it runs on it. This output have the purpose to print in word afterword. While i was building my project i didn't notice something that happens with the fetching part ,it was after i print it in word that i noticed something strange.

I execute a select statement and fetches this in an arrayref , but when i print this arrayreferentie I don't got only values in my array but also empty strings and de cli gives me some error code with it. I firstly though to just chomp after i read a line so that it wil go to the next line but that didn't work . Is there anyone who have come to this problem already ?

This is my code:

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();

The output of the print in my CLI is as followed

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

[EDIT]

$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'
          ]
        ];

1条回答
混吃等死
2楼-- · 2019-07-24 03:43

I found the solution to my own question , for future readers:

My 2 loops where not correctly done (I used a foreach and a while) the following code did the job.

 foreach my $row (@$databases)
        {

            my $databaseColumnNumber =0;
             foreach my $val (@$row) 
             {
                print "\n" , $val;
            }

        }
查看更多
登录 后发表回答