in reply to Re: MySQL INSERT in Perl
in thread MySQL INSERT in Perl

I did use

use strict;

unfortunately, when I use it, I get the following error:
Global symbol "$ins_rec" requires explicit package name at otest.pl li +ne 53. Global symbol "$ins_rec" requires explicit package name at otest.pl li +ne 55. Execution of otest.pl aborted due to compilation errors.
so I commented it out... how does $ins_rec go out of scope?

Replies are listed 'Best First'.
Re^3: MySQL INSERT in Perl
by NetWallah (Canon) on Aug 04, 2011 at 17:46 UTC
    OK - strict, did identify your problem, which does indeed show a scoping issue.

    Do you have BLOCKS in your code - i.e - segments of code enclosed in {curly braces} ?

    Is the "my $ins_rec" line inside one of these blocks ?
    If so, that variable is not visible outside the block - you need to move the "my" statement outside the block.

    Please do yourself a favour and ALWAYS "use strict;" and "use warnings;". Professional programmers recommend this for very good reasons.

                "XML is like violence: if it doesn't solve your problem, use more."

      it was indeed a scoping problem...I fixed it. it runs and it inserts records into the database, but it gives a new error now:
      DBD::mysql::db do failed: Unknown column 'N' in 'field list' at otest. +pl line 52.
      I am perplexed...it says do failed, but it did insert the records and it does it all correctly...then why the error? this is my updated code:
      my $dbh=DBI->connect($dsn,$user,$pwd); foreach my $hr1 (@calls) { my %h1 = %{$hr1}; next unless defined $h1{'symbol'}; my $ins_rec="INSERT INTO $table (symbol,valuation_dt,expn_dt,strik +e,open,bid,ask,last,volume) VALUES(\'$h1{'symbol'}\',now(),\'$exp\',$ +h1{'strike'},$h1{'open'},$h1{'bid'},$h1{'ask'},$h1{'last'},$h1{'volum +e'});"; print "my ins_rec is $ins_rec\n"; my $sth=$dbh->do($ins_rec); }

        Stop pasting values into your SQL. Use placeholders. Now!

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)