我想两次使用查询的结果.
如何重新定位指针以便从第二次开始读取结果?
以下示例(为简单起见,只需打印到屏幕):
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";
}
Run Code Online (Sandbox Code Playgroud)
您不希望IO库将整个文件加载到内存中以逐行读取它,那么为什么您希望它来自数据库库?
此外,这完全没必要.加载完整的结果非常容易.
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;
...
}
Run Code Online (Sandbox Code Playgroud)
如果需要DBI,可以将数据加载到DBD :: Sponge中.