Roger Perttu wrote:

>  Hi everyone,
>
> I have a problem with DBI. If I have no control of the SQL statement 
> being run, how do I know if it's safe to call fetchrow_hashref()?
>
> my $sth = $dbh->prepare($someUnknownStatement);
> $sth->execute();
> # Safe to call fetchrow_hashref()?
>
> I know I can turn of RasieError and PrintError but I want to catch 
> _real_ errors. I could also use eval but should I then search the 
> error string for "no select statement currently executing" and ignore 
> the error if I find it. That would be very ugly code. Surely there 
> must be a better way. I don't understand why fetchrow_hashref() just 
> doesn't return undef.
>
> Thanks,
> Roger Perttu
>
You can (untested)

if ($someUnknownStatement =~ /^\s*SELECT/i) { while 
($sth->ftechrow_hashref) {...code...}},

but this will not work for proprietary or non-SQL statements. If you 
really have * no* control of the statement you will have to eval() 
anyways (statement syntax errors etc).

Bodo


Reply via email to