Chinaunix

标题: DBI::st??? [打印本页]

作者: smilesky    时间: 2003-04-14 13:50
标题: DBI::st???
要实现的功能如下:从数据库中取值,最多一次取5个。
数据库中数据是否为空,等待5秒钟后程序重新启动从数据库取值。

代码如下:
.........
my $sql = qq{ SELECT * from cmd limit 5};

my $sth = $db->;prepare( $sql );

if ($sth->;numrows == 0)
        {
         sleep(5);
         next;
        }
$sth->;execute();

为什么报错为:Can't locate object method "numrows" via package "DBI::st" at ./action_00.pl line 29.

我应该怎么修改?
作者: apile    时间: 2003-04-14 18:21
标题: DBI::st???
你還沒有$sth->;execute;
怎麼就會知道有多少 row???
另外沒有這個numrows的method...
你在去看一下document 查清楚點吧..
作者: deathcult    时间: 2003-04-14 18:39
标题: DBI::st???
没有这个方法。
作者: apile    时间: 2003-04-14 21:28
标题: DBI::st???
你的問題解法..
use DBI;
&DB_CON;

$sql="SELECT XXXX from table where condition";
Retry:
$sth=$db->;prepare(qq{ $sql })
    || die "Can't prepare $sql: $DBI::errstr";
$sth->;execute || die "can't execute sql: $DBI::errstr";

$n=0;
$flag=0;
while(@row=$sth->;fetchrow_array()){
       $flag=1;
        $n++;
      ## do something with @row
        if($n==5){ last;}
}
# 沒數據的時候
if($flag ==0){
sleep(5);
goto Retry;
}

如果數據一直是空的,上面會是個無窮迴圈沒完沒了...
我沒debug過..你自己debug與加上其他程序碼吧...
雖然很多書上都建議不要用goto..不過goto偶爾
還是有點用處的..

另外你的程序碼中,一定要先fetchrow_array;
你那個類似的numrows的method才會累加..不然都是0....
作者: smilesky    时间: 2003-04-15 16:51
标题: DBI::st???
谢谢

原帖由 "apile" 发表:
你的問題解法..
use DBI;
&DB_CON;

$sql="SELECT XXXX from table where condition";
Retry:
$sth=$db->;prepare(qq{ $sql })
    || die "Can't prepare $sql: $DBI::errstr";
$sth->;execute || die..........

作者: deathcult    时间: 2003-04-16 11:51
标题: DBI::st???
你的数据库连接一直保持着? :)
作者: apile    时间: 2003-04-16 12:22
标题: DBI::st???
程序結束..connection會自動斷掉..所以一般dbi的document..
有提過寫不寫disconnection的code無所謂..perl會自動中斷..
作者: deathcult    时间: 2003-04-16 17:17
标题: DBI::st???
hi apile  :

我是说他程序设计的问题,不是说你写的代码问题 ;)
作者: apile    时间: 2003-04-16 22:52
标题: DBI::st???
sorry..不好意思..我誤解了...




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2