0

I am accessing the Database and using the fetchrow_array() & fetchrow_hashref methods, I am getting the error:

DBD::mysql::st fetchrow_array failed: fetch() without execute() at ./aaa.pl line 83.

I am not getting why I am getting this error, my tool is doing what it supposed to do do but in the middle of my execution it is giving me this line.

#!/usr/bin/perl

use DBI;
use strict;

use warnings;
use Switch;
use Data::Dumper;
my $dbType = "mysql";
my $database = "control";
my $host = "localhost";
my $dsn = "";
my $userid = "";
my $password = "";

my $dbDescription = "";
my $dbSa_Query = "";
my $dbttid = "";
my $dbtidv = "";
my $dbti = "";
my $dbrhostfqdn = "";
my $dbDialedNumber = "";
my $dbPortNumber = "";
my $id = 42;
my % carriers;
my % preTranslation;
my % resourceGroup;

sub dataBaseOperation() {

        if ($dbType eq "mysql") {
                #$database = "trustid";
                $dsn = "DBI:$dbType:database=$database;host=$host:port=3306,DBSOCK = '/var/run/mysqld/mysqld.sock'";
                $userid = "XXXX";
                $password = 'XXXX';
        } else {
                $dbType = "postgres";
                $userid = "postgres";
                $password = "postgres";
                $dsn = "DBI:$dbType:database=$database;host=$host:port=5432";
        }

        my $dbcon = DBI -> connect($dsn, $userid, $password) or die "Can't connect to database: $DBI::errstr\n";
        print "connected to the database\n";

        my $notables = $dbcon -> tables();
        print "No of tables : $notables".
        "\n";
        my @tables = $dbcon -> tables(undef, undef, undef, 'TABLE');

        my $len = @tables;
        for (my $i = 0; $i < $len; $i = $i + 1) {
                print("\@tables[$i] = $tables[$i]\n");
                print("\n");
        }
        my $query = 'select * from active_table_set';
        my $sth = $dbcon -> prepare($query) or die "Unable to prepare $query".$dbcon -> errstr;

        $sth -> execute() or die "Unable to execute '$query'.  ".$sth -> errstr;

        while (my @row = $sth -> fetchrow_array()) {
                $database = join(',', @row);
                print($database.
                        "\n");
                if ($dbType eq "mysql") {
                        #$database = "trustid";
                        $dsn = "DBI:$dbType:database=$database;host=$host:port=3306,DBSOCK = '/var/run/mysqld/mysqld.sock'";
                        $userid = "XXXX";
                        $password = 'XXXX';
                } else {
                        $dbType = "postgres";
                        $userid = "postgres";
                        $password = "postgres";
                        $dsn = "DBI:$dbType:database=$database;host=$host:port=5432";
                }
                $dbcon = DBI -> connect($dsn, $userid, $password) or die "Can't connect to database: $DBI::errstr\n";
                print "connected to the database\n";
                @tables = $dbcon -> tables(undef, undef, undef, 'TABLE');
                $len = @tables;
                for (my $i = 0; $i < $len; $i = $i + 1) {
                        print("\@tables[$i] = $tables[$i]\n");
                        print("\n");
                        if ($tables[$i] eq '`trustid_b`.`carriers`') {
                                $sth = $dbcon -> prepare('select * from carriers');

                                $sth -> execute();

                                while (my $row = $sth -> fetchrow_hashref) {
                                        $carriers {
                                                $row -> {
                                                        description
                                                }
                                        } = $row;
                                }
                        }
                }
                @tables = $dbcon -> tables(undef, undef, undef, 'TABLE');
                $len = @tables;
                for (my $i = 0; $i < $len; $i = $i + 1) {
                        if ($tables[$i] eq '`trustid_b`.`pre_translation`') {
                                $sth = $dbcon -> prepare('select * from pre_translation');

                                $sth -> execute();
                                while (my $row = $sth -> fetchrow_hashref) {
                                        $preTranslation {
                                                $row -> {
                                                        id
                                                }
                                        } = $row;
                                }
                        }
                }

                @tables = $dbcon -> tables(undef, undef, undef, 'TABLE');
                $len = @tables;
                for (my $i = 0; $i < $len; $i = $i + 1) {
                        if ($tables[$i] eq '`trustid_b`.`resource_group`') {
                                $sth = $dbcon -> prepare('select * from resource_group');

                                $sth -> execute();
                                while (my $row = $sth -> fetchrow_hashref) {
                                        $resourceGroup {
                                                $row -> {
                                                        description
                                                }
                                        } = $row;
                                }
                        }
                }

                $dbcon -> disconnect;
        }
        $dbcon -> disconnect;
        print("\n".
                "Starting Database Operations.".
                "\n");
        dataBaseXLCompare();
        print "\n";

}

I Have tried to print it and kill it but nothing seems to be working.

line number where I am getting error -->

while (my @row = $sth -> fetchrow_array()) {

Why I have used Two connect, Because it is giving me proper output. I think a DB information is present table of another db. I didn't have info of database. I have accessed it using my code and aimed to open carrier, pretranslation and resource table. It is because of below Image. Why Used tow conections

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • I have tried to resolve it using this question -->> https://stackoverflow.com/questions/7598808/dbdmysqlst-fetchrow-array-failed-fetch-without-execute – Lalit Kumar Singh Jun 06 '22 at 06:20
  • 1
    why so many connections to the "apparent" same DB? One connection per operation type (SELECT vs UPDATE/DELETE/INSERT) is sufficient. Note that you are overwritting $sth in the inner loops. Suggest reducing the nesting code. – PandaCheLion Jun 06 '22 at 06:34
  • @chegancasb I also didn't get but it is actually giving me the proper results, I think it may be a db store one table which is having info of other db. Starting Database Operations. connected to the database No of tables : 1 @tables[0] = `control`.`active_table_set` trustid_b connected to the database @tables[0] = `trustid_b`.`ani_pre_translation` @tables[1] = `trustid_b`.`ani_pre_translation_test – Lalit Kumar Singh Jun 06 '22 at 06:38
  • 1
    Check the return values of your execute() calls. – clamp Jun 06 '22 at 09:25
  • @clamp sir, can you please guide me how to do that? – Lalit Kumar Singh Jun 06 '22 at 10:50

1 Answers1

1

After going inside the while loop, it is processing it according to the code, During second iteration it is trying to fetch another row using fetchrow_array() method which is causing this error.

add, last just before closing of while loop so that loop won't get iterated.

last;