REGISTER  


Perl DBI (DBD::Pg) and Postgresql error message "no statement executing"
This thread belongs to soger.forumgeeks.net


2009-03-07 13:07 GMT   |   #1

Comments: 42

I am using the perl DBI to connect to a postgres database.
From time to time I get the error message: no statement executing
however it seems to me that the query itself is executed, only the error is returned, but this prevents my script to continue.
Did anyone meet this error?

2009-03-08 14:38 GMT   |   #2
 

Interesting...
This usually means that you do not use the same statement handle (usually denoted with $sth) for the prepare() and the execute() or fetch()

Does this help you?

2009-03-11 08:19 GMT   |   #3

Comments: 42

I have a function that does all of these. It looks like this:

sub execsql
{
    my $sth=$dbi->prepare($sql);
    if (!$sth)
    {
        return undef;
    }
    my $rv=$sth->execute;
    if (!defined $rv)
    {
        return undef;
    }
    my $ref=$sth->fetchall_arrayref;
    if ($sth->err)
    {
        return undef;
    }
    return $ref;
}
I removed the error handling so that the function is simpler but this is the basic idea.

2009-03-11 10:06 GMT   |   #4
 

Well this looks OK, can you give me an example what query causes the error?

2009-03-14 02:14 GMT   |   #5

Comments: 42

sure:

update badges set user_categs=get_user_categs(57) where id=57

Where the get_user_categs() is a function that returns and integer[]

 

2009-03-14 16:49 GMT   |   #6
 

Ok, I got it.

You call fetchall_arrayref() on a query that does not return anything.
For UPDATE and DELETE queries you should not call the fetchall_arrayref() function or it even might be more optimal to call the do() function.
Here is an example, really simple:

sub dosql {
    my $rv=$dbi->do($_[0]);
    if (!defined $rv) {
        return undef;
    }
    return $rv;
}



Last edited by deviant at 2009-03-17 11:37 GMT
2009-03-17 11:29 GMT   |   #7

Comments: 42

Thank you, the do() function really did solve my problem.