in reply to Re: Perl DBI execute statement
in thread Perl DBI execute statement

Thank you, the code is what I wanted, but I have figured out my issue, I get the following error:

DBD::mysql::st fetchrow_array failed: fetch() without execute() at /ho +me/rdscopy.pl line 204. DBD::mysql::st fetchrow_array failed: fetch() without execute() at /ho +me/rdscopy.pl line 204.

when there is no data in the table facebook_321_ext table in production, so the query reutrns nothing and then the insert statement below " while (my @insert_ext = $feed_table_ext_results->fetchrow_array())" which is line 204 gives that error, but it works when data is there upon other test.

while (my @insert_ext = $feed_table_ext_results>fetchrow_array()) { $ext_insert->execute(@insert_ext) ; }
Is there a way I can error check before executing?

Replies are listed 'Best First'.
Re^3: Perl DBI execute statement
by huck (Prior) on Dec 12, 2016 at 20:51 UTC

    as poj has already pointed out "I suspect there are other problems" but you have at least successfully identified that you get that error when $feed_table_idcode->fetchrow_array does not return any rows, because then $feed_table_ext_results->execute never gets executed. One way to test if it has returned any rows is to set some sort of flag to false as a default state, and then set it to true if $feed_table_idcode->fetchrow_array did return a row, (and $feed_table_ext_results->execute gets executed). Then you can skip the $feed_table_ext_results>fetchrow_array loop if your flag is still false

    for instance $did_ex_flag=0; sets a flag to false, and $did_ex_flag=1; sets it to true, and if ($did_ex_flag) { ...} can be used to test it.

    another way, that might be more useful, would be to move the $feed_table_ext_results->fetchrow_array loop to right after the $feed_table_ext_results->execute statement. Of course you now realize the $feed_table_ext_idcode = statement needs to be before that point.

      Ok, thank you , will try using the flags method .
Re^3: Perl DBI execute statement
by poj (Abbot) on Dec 12, 2016 at 19:48 UTC
    Is there a way I can error check before executing?

    Yes, but I suspect there are other problems. How many lines are there in the whole script ? You need to show how the 3 execute statement are nested together

    poj

      currently there are 312 lines.

      My insert statements are
      #Prepare insert statement for feed table $sth_insert = $dbh3->prepare("INSERT IGNORE INTO $feed_table (id_c +ode, entry_time, parent_id_code, author_name, author_code, author_url +, author_image_url, entry_url, entry_types, status_code, entry_text, +entry_data, last_update, pull_time, queue_code) VALUES (?, ?, ?, ?, ? +, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") or die $dbh3->errstr; ##prepare insert statement for feed table ext my $ext_insert = $dbh3->prepare("INSERT IGNORE INTO $feed_table_ex +t (id_code, item_name, item_value, item_data) VALUES (?, ?, ?, ?)") o +r die $dbh3->errstr; ##prepare insert table for feed table category my $cat_insert = $dbh3->prepare("INSERT IGNORE INTO $feed_table_ca +tegory (id_code, category_class_code, category_code, user_name, cat_t +ime) VALUES (?, ?, ?, ?, ?)") or die $dbh3->errstr; ##prepare insert table for feed table log my $log_insert = $dbh3->prepare("INSERT IGNORE INTO $feed_table_lo +g (id_code, action_time, action_usec, action_code, action_type, user_ +id, processor_id, action_text) VALUES (?, ?, ?, ?, ?, ?, ?, ?)") or d +ie $dbh3->errstr; #get table results from production stream table my $activate_info= $dbh->prepare("SELECT source_code, stream_code, cli +ent_id, stream_oauth_id, name, url, status_code, stream_data, feed_db +, feed_table, last_update, report_db, sla, exclude_request, owned, pu +sh FROM stream WHERE id = ?"); $activate_info->execute($client_ID); ##prepare the insert statement $activate_stream = $dbh4->prepare("INSERT INTO stream (source_code, s +tream_code, client_id, stream_oauth_id, name, url, status_code, strea +m_data, feed_db, feed_table, last_update, report_db, sla, exclude_req +uest, owned, push) VALUES(?, ?, 10, 1, ?, ?, \'DISABLED\', ?, \'socia +l_patrol_feed002\', ?, ?, ?, ?, ?, ?, ?)"); ##Insert statement execution print "Dropping $feed_table table..\n"; #sleep(3); $dbh3->do("DROP TABLE IF EXISTS $feed_table"); print "Recreating $feed_table table..\n"; #sleep(3); my $qy = $dbh2->prepare("SHOW CREATE TABLE $feed_table"); $qy->execute(); my $feed_table_create = $qy->fetchrow_array(); $dbh3->do("$feed_table_create"); print "Inserting data into $feed_table table..\n"; #sleep(3); while (my @insert = $table_results->fetchrow_array()) { $sth_insert->execute(@insert) or die $sth_insert->errstr; } #$sth_insert->finish(); ##Insert ext execution print "Dropping $feed_table_ext table..\n"; #sleep(3); #$dbh3->do("DROP TABLE IF EXISTS $feed_table_ext"); print "Recreating $feed_table_ext table..\n"; #sleep(3); #my $qy_ext = $dbh2->prepare("SHOW CREATE TABLE $feed_table_ext"); #$qy_ext->execute(); #my $feed_table_ext_create = $qy_ext->fetchrow_array(); # $dbh3->do("$feed_table_ext_create"); print "Inserting data into $feed_table_ext table..\n"; #sleep(3); while (my @insert_ext = $feed_table_ext_results->fetchrow_array()) { #print "@insert_ext\n"; $ext_insert->execute(@insert_ext) ; } # $ext_insert->finish(); ##Insert category execution print "Dropping $feed_table_category table..\n"; #sleep(3); $dbh3->do("DROP TABLE IF EXISTS $feed_table_category"); print "Recreating $feed_table_category table..\n"; #sleep(3); my $qy_cat = $dbh2->prepare("SHOW CREATE TABLE $feed_table_category"); $qy_cat->execute(); my $feed_table_cat_create = $qy_cat->fetchrow_array(); $dbh3->do("$feed_table_cat_create"); print "Inserting data into $feed_table_category table..\n"; #sleep(3); while (my @insert_cat = $feed_table_category_results->fetchrow_array() +) { $cat_insert->execute(@insert_cat) or die $sth_insert->errstr; } # $cat_insert->finish(); ##Insert log table execution print "Dropping $feed_table_log table..\n"; #sleep(3); $dbh3->do("DROP TABLE IF EXISTS $feed_table_log"); print "Recreating $feed_table_log table..\n"; #sleep(3); my $qy_log = $dbh2->prepare("SHOW CREATE TABLE $feed_table_log"); $qy_log->execute(); my $feed_table_log_create = $qy_log->fetchrow_array(); $dbh3->do("$feed_table_log_create"); print "Inserting data into $feed_table_log table..\n"; #sleep(3); while (my @insert_log = $feed_table_log_results->fetchrow_array()) { $log_insert->execute(@insert_log) or die $sth_insert->errstr; } # $log_insert->finish(); ##Prepare insert statement to activate stream if($sth_insert->errstr) { print "The insert statement failed for data $sth_insert->errstr"; } else { ##Insert statement execution to activate stream #print "Will now delete data from stream table with client ID +$client_ID\n"; #my $stream_delete = $dbh4->prepare("DELETE FROM stream WHERE id = ?") +; #$stream_delete->execute($client_ID); print "Inserting data into STREAM table..\n"; sleep(3); while (my @activate_results = $activate_info->fetchrow_array() ) { $activate_stream->exe +cute(@activate_results[0, 1, 4, 5, 7, 9 .. 15]) or die $activate_stre +am->errstr; } + } #$activate_stream->finish();

        Where does this loop

        while (my @row = $feed_table_ext_idcode->fetchrow_array()) { #print "@row\n"; $feed_table_ext_results->execute(@row); }

        appear in relation to this loop ?

        while (my @insert_ext = $feed_table_ext_results->fetchrow_array()){ #print "@insert_ext\n"; $ext_insert->execute(@insert_ext) ; }

        I was expecting to see the second nested in the first

        poj
Re^3: Perl DBI execute statement
by Marshall (Canon) on Dec 14, 2016 at 20:27 UTC
    This error message is saying that:
    my $nRows_affected = $feed_table_ext_results->execute(..)
    was never executed. Why that is, I don't know at the moment, but it appears that there a number of posts that address this issue.

    Please adjust the formatting of your code to something similar to this:

    while (my @insert_ext = $feed_table_ext_results->fetchrow_array()) { $ext_insert->execute(@insert_ext) ; }
    Instead of what you posted:
    while (my @insert_ext = $feed_table_ext_results>fetchrow_array()) { $ext_insert->execute(@insert_ext) ; }
    The Perl compiler doesn't care about huge indentations of the braces, but the humans do care.