luke devon wrote:
> Hi Friends, 
> 
> Here, I am trying to connect sqlite DB and access some data for matching 
> values with STDIN.But when i try to debug the code it gives following
> message. I went through google , and couldn't find any solution yet. Can some
> body help me pelase.
> 
>  
> closing dbh with active statement handles at xxx.pl line 48, <STDIN> line 1.
> 
> This is my perl code, 
> 
> #!/usr/bin/perl
> use DBI;
> use strict;
> use warnings;
> #
> ## no buffered output, auto flush
> $|=1;
> 
> my ($dbh,$sth,$dbargs,$VALUEB,$query);
> 
> $dbargs = {AutoCommit => 0, PrintError => 1};
> $dbh = DBI->connect("dbi:SQLite:dbname=List","","",$dbargs);

It's best to declare variables at the point of first use. Otherwise the benefits
of using strict start to be diluted.

  my $dbargs = {AutoCommit => 0, PrintError => 1};
  my $dbh = DBI->connect("dbi:SQLite:dbname=List","","",$dbargs);

Why no autocommit?

> if ($dbh->err()) {
>    print;
>    exit;
> }

There's nothing special in $_ to print, so if you're relying on DBI having
already raised an error then just

  exit if $dbh->err;

> while (<STDIN>) {
>    chomp;
>    my ($url, $url2, $ip) = split(/ /);

You want

  my ($url, $url2, $ip) = split;

othwerise multiple or leading spaces in the input will mess things up.

>    $query = "SELECT * from Groups where XvalueAddress = '". $ip ."'";
>    $sth = $dbh->prepare($query);
>    $sth->execute();

  my $query = 'SELECT * from Groups where XvalueAddress = ?';
  my $sth = $dbh->prepare($query);
  $sth->execute($ip);

> 
>    if (my $ref = $sth->fetchrow_hashref()) {
>        $VALUEB = $ref->{'CallId'};
>        #if (!defined $VALUEB) {
>        #   $VALUEB = "NA";
>        #}
>    }else{
>       $VALUEB = "NA";
>    }

I suggest you call

  $sth->finish;

here. But you might find that everything's OK without calling it at all because
it will be destroyed automatically at the end of this execution of the while 
loop.

You might even consider just:

   my ($VALUEB) = $dbh->selectrow_array(
       'SELECT CallId FROM Groups WHERE XvalueAddress = ?',
       undef, $ip);

as there's no point in in calling $sth->prepare and $sth->execute unless you're
reusing the statement handle, which your code doesn't do.

>    if (!($url =~ m#valueB#)) {
>        if ($url =~ m#\?#) {
>           $url .= ("&valueB=" . $VALUEB);
>        } else {
>           $url .= ("?valueB=" . $VALUEB);
>        }
>        print $url."\n";
>    } else {
>        print "\n";
>    }

Using the standard delimiters for a regex is recommended unless there's a
conflict with the contents of the expression. And 'cuddled' elses aren't
recommended in

  perldoc perlstyle

although obviously it's up to you whether you agree.


  if ($url !~ /valueB/) {
    if ($url =~ /\?/) {
      $url .= "&valueB=$VALUEB";
    }
    else {
      $url .= "?valueB=$VALUEB";
    }
      print "$url\n";
    }
    else {
      print "\n";
    }
  }

But you should consider making use of the URI module to build queries like this.

> }
>    $sth->finish();
>    $dbh->commit();
>    $dbh->disconnect();

It doesn't look like it should give you that error, as you've called
$sth->finish before the disconnect, but one of the options above should help.

HTH,

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to