1

クエリの結果を 2 回使用したいと考えています。
ポインターの位置を変更して、結果をもう一度最初から読み取り始めるにはどうすればよいですか?
以下の例 (簡単にするために画面に出力するだけです):

if ( $dbh = DBI->connect( "DBI:mysql:database=tng;host=ip", "username", "password" ) ) {
    $strSQL = "select * from table";
    if ( $strQuery = $dbh->prepare($strSQL) ) {
        if ( $strQuery->execute() ) {
            while ( @data = $strQuery->fetchrow_array() ) {
                print $data[0];
            }
            --reposition to top and reread the query result and do something else with the data-- $strQuery->finish;
        }
        else {
            $strMsg = "$strDateTime ERROR -- unable to execute statement: " . $strQuery->errstr . "\n";
            print logFile "$strMsg";
        }
    }
    else {
        $strMsg = "$strDateTime ERROR -- unable to prepare statement: " . $dbh->errstr . "\n";
        print logFile "$strMsg";
    }
    $dbh->disconnect();
}
else {
    print logFile "$strDateTime ERROR -- unable to connect to iptables database ... " . DBI->errstr . " \n";
}
4

2 に答える 2

5

IO ライブラリがファイル全体をメモリにロードして 1 行ずつ読み取るとは思わないのに、なぜデータベース ライブラリからそれを期待するのでしょうか?

その上、それはまったく不要です。完全な結果をロードするのは非常に簡単です。

my $sth = $dbh->prepare($sql);
my $data = $dbh->selectall_arrayref($sth);

for my $row (@$data) {
    my ($col1, $col2, ...) = @$row;
    ...
}

for my $row (@$data) {
    my ($col1, $col2, ...) = @$row;
    ...
}

DBI sth が必要な場合は、データをDBD::Spongeにロードできます。

于 2012-04-17T23:04:35.417 に答える
1

これを行うことはできないと言おうとしましたが、できるように見えますexecute。もう一度ステートメントを処理する必要があります。このコードは PostgreSQL を使用することに注意してください。これは、私が PostgreSQL を使用しているためです (また、MySQL のインストールが手元にありません) が、これも機能するはずです。

use strict;
use warnings;
use DBI;
my $dbh=DBI->connect("dbi:Pg:dbname=db;host=hosty-host-host","username","password")
  or die DBI->errstr;

my $sth=$dbh->prepare("select generate_series(1,5)") or die $dbh->errstr;
$sth->execute or die $dbh->errstr;

while(my @row=$sth->fetchrow_array)
{
  print "$row[0]\n";
}

print "\nSecond verse, same as the first!\n\n";
$sth->execute or die $dbh->errstr;

while(my @row=$sth->fetchrow_array)
{
  print "$row[0]\n";
}

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

出力は次のとおりです。

1
2
3
4
5

Second verse, same as the first!

1
2
3
4
5

ちなみに、大量の if-else ステートメントを使用するよりも、 errand/orerrstrを参照してエラー チェックを行うことをお勧めします。

編集して追加:ステートメントを再度処理し たくない場合execute(およびデータベースから情報を再読み取りしたくない場合) は、おそらく a) データを (Perl) データ構造に格納するか、b) ファイルに書き込むかのいずれかで行き詰っています。次に、ファイルから読み込みます (そしてseek、ファイル ハンドルを好きなだけ使用します)。

于 2012-04-17T22:02:34.910 に答える