How do I access a SQL Server database from a Perl script in Linux?

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

    How do I access a SQL Server database from a Perl script in Linux?

    I'm not able to insert the value into a SQL Server database from a Perl script in Linux
    code is given below:

    #!/usr/bin/perl

    use strict;
    use DBI;
    use Data::Dumper;
    use Asterisk::AGI;

    my $agi = new Asterisk::AGI;

    my $fifth_param = "22";
    my $first_param = "04-09-2012";
    my $second_param = "04-09-2012";
    my $third_param = "2002";
    my $forth_param = "0123";

    my $DSN = q/dbi:ODBC:SQLSER VER/;
    my $uid = q/ivr/;
    my $pwd = q/ivr/;

    my $DRIVER = "Freetds";
    my $dbh = DBI->connect($DSN,$ uid,$pwd) or die "Coudn't Connect SQL";
    # my $servernumber = 2;

    # my ($dbname) = $dbh->selectrow_arra y("select DATABASE()");
    eval
    {
    my $td = $dbh->do(q/USE CServer/);
    # print "$td\n";
    };

    my $sql = "Insert into dbo.winast_outb ound_callmaster (out_call_numbe r,out_start_tim e,out_end_time, out_extenstion, out_CLI) values('$fifth_ param','$first_ param','$second _param','$third _param','$forth _param')";


    my $sth = $dbh->prepare($sql );
    my $sth->execute() or die $dbh->errstr;


    my $return = $sth->fetchrow();
    my $agi->set_variable(' result',$return );
    print Dumper($return) ;


    $sth->finish;
    $dbh->disconnect;


    I got this error :-

    Can't call method "execute" on an undefined value at Outgoing_Call.a gi line 45.
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Your execute statement is on line 35 and there is no line 45 in the code you posted, so that error message was not generated by the script you posted.

    Why are you using that eval block? Take that out and instead specify the database in the connect statement.

    You should also add vertical whitespace to the sql statement to add readability and, you should use placeholders and put the values in the execute statement.

    Comment

    • RonB
      Recognized Expert Contributor
      • Jun 2009
      • 589

      #3
      I also suggest that you adjust your error handling. As is, it's ok for command line testing, but when used as an agi script, you won't see those error messages in either the asterisk cli or cdr file.

      Comment

      • omakhileshchand
        New Member
        • May 2012
        • 13

        #4
        Originally posted by RonB
        Your execute statement is on line 35 and there is no line 45 in the code you posted, so that error message was not generated by the script you posted.

        Why are you using that eval block? Take that out and instead specify the database in the connect statement.

        You should also add vertical whitespace to the sql statement to add readability and, you should use placeholders and put the values in the execute statement.
        Sir,

        how can i identify that table is belong in which database that why i have used eval statement.In my sql server have many databases.
        I have put vertical whitespace and placeholders but it is showing same error.
        Please help me..

        Comment

        • omakhileshchand
          New Member
          • May 2012
          • 13

          #5
          i got the solution thanks for the help

          Comment

          Working...