开发者

mysql , perl script freezing when retrieving data

开发者 https://www.devze.com 2023-02-15 03:53 出处:网络
Hi I have this开发者_开发知识库 code. my perl scripts hangs somewhere in the @row section. I printed the next query statement and it works in SQL.

Hi I have this开发者_开发知识库 code. my perl scripts hangs somewhere in the @row section. I printed the next query statement and it works in SQL.

Why is it hanging?

foreach $device (...)

        $sth2 = $dbh->prepare(qq|SELECT DISTINCT S,`W(m)`,`L(m)`,V FROM `$SQL_TABLE_NAME` WHERE DEVICE='$device'| );
        $sth2->execute(  );
        my %TEMP = ();
        while ( my @row = $sth2->fetchrow_array(  )) 
        {
            $TEMP{S}{$row[0]} = 1;
            $TEMP{W}{$row[1]}       = 1;
            $TEMP{L}{$row[2]}       = 1;
            $TEMP{V}{$row[3]}     = 1;

        }


You seem to have a syntax error in your query (VFROM rather than V FROM).

If the query fails, there's no result set to fetch a row from. You might want to build some error handling into your code.


my $select_line = qq|SELECT DISTINCT S,`W(m)`,`L(m)`,V 
 FROM `$SQL_TABLE_NAME` 
 WHERE DEVICE='$device'|;

Have you tried printing the $select_line and then running it directly in mysql?

my $sth2 = $dbh->prepare( $select_line ) 
 or die $DBI::errstr.' at my query: $select_line\n';
  1. Add in or die $DBI::errstr.' at my query: $select_line' to verify if your syntax is correct.
  2. Add in die $sth2->errstr if $sth2->err; after your while:

    while (fetch) {

    //stuff in here with rows

    } die $sth2->errstr if $sth2->err;

  3. Review CPAN DBI docs @ http://metacpan.org/pod/DBI


By the way it's better to use placeholders:

$sth2 = $dbh->prepare(qq|SELECT DISTINCT S,`W(m)`,`L(m)`,V FROM `$SQL_TABLE_NAME` WHERE DEVICE= ?| );
$sth2->execute($device);
0

精彩评论

暂无评论...
验证码 换一张
取 消