unable to execute insert command in perl script: Can't call method on undefined value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omakhileshchand
    New Member
    • May 2012
    • 13

    unable to execute insert command in perl script: Can't call method on undefined value

    Code:
    #!/usr/bin/perl
             # Here Module's are define
             use DBI;
                                                             # For database Connectivity
             my $DSN = q/dbi:ODBC:SQLSERVER/;
             my $uid = q/ivr/;
             my $pwd = q/ivr/;
             
            
             my $DRIVER = "Freetds";
             my $dbh = DBI->connect($DSN,$uid,$pwd) or die "Coudn't Connect SQL";
    
    $fund_code=103;
    $mobile_number='7702004546';
    $city_address='Karvy Computershare Private Limited,2E / 23, Jhandewalan Extn New Delhi - 110055';
    
     $sql_query4 = "INSERT INTO [192.168.14.28].CommunicationLog.dbo.sms_processedfeeds (pf_Fund,pf_trtype,pf_acno,pf_ihno,pf_Mobile,pf_msgtrtype,pf_msg,pf_entdt ) VALUES (?,?,?,?,?,?,Getdate())";
                  $sql_sms = $dbh->prepare($sql_query4);
                  $sql_sms->execute($fund_code,'CALL', 0, undef, $mobile_number, 'CALL',$city_address,);
    whenever i'm executing the code, i have gotten following error:

    Can't call method "execute" on an undefined value at M_CHECK_SMS_STD CODE.pl line 19
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    First problem, which is the most important, is that you didn't enable strictures and warnings. You enable them by adding the strict and warnings pragmas to the beginning of EVERY script you write.

    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use Carp;  # not actually required, but is one of my preferences.
    The next problem, unrelated to your error, is your improper use of indentation and lack of proper use of horizontal/vertical white space.

    The cause of your error is that the prepare statement failed, but due to the lack of error checking, you implicitly told perl not tell you that it failed.

    Here's an adjusted version which should shed some light on the reason it failed.
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use Carp;
    use DBI;
    
    my $DSN    = q/dbi:ODBC:SQLSERVER/;
    my $uid    = q/ivr/;
    my $pwd    = q/ivr/;
    my $DRIVER = "Freetds";
    my $dbh    = DBI->connect($DSN,$uid,$pwd, { RaiseError => 1 })  # enables error checking on all db calls
               or croak "Coudn't Connect SQL " . $DBI::errstr;
           
    my $fund_code     = 103;
    my $mobile_number = '7702004546';
    my $city_address  = 'Karvy Computershare Private Limited,2E / 23, Jhandewalan Extn New Delhi - 110055';
    
    my $sql_query4 = "INSERT INTO [192.168.14.28].CommunicationLog.dbo.sms_processedfeeds (pf_Fund,
                                                                                           pf_trtype,
                                                                                           pf_acno,
                                                                                           pf_ihno,
                                                                                           pf_Mobile,
                                                                                           pf_msgtrtype,
                                                                                           pf_msg,pf_entdt)
                    VALUES (?,?,?,?,?,?,Getdate())";
    
    my $sql_sms = $dbh->prepare($sql_query4);
    
    $sql_sms->execute($fund_code,'CALL', 0, undef, $mobile_number, 'CALL',$city_address,);

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Also, I just noticed that in the execute() function, you are providing 7 values, but the "VALUES" portion of your query only has 6 "?" characters. I am not sure if that matters, but it may.

      Comment

      Working...