Quantcast

Error handling

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Error handling

Igor Korot
Hi, ALL,
Consider the following pseudo-code:

[code]
int ret = sqlite3_prepare_v2();
if( ret != SQLITE_OK )
{
// henerate error message
return 1;
}
while( ; ; )
{
    ret = sqlite3_step();
    if( ret == SQLITE_ROW )
    {
// everything is good
    }
    else if( ret == SQLITE_DONE )
        break;
    else
    {
        result = 1;
        break;
    }
}
if( ret != SQLITE_DONE )
    return 1;
// everything is good - continue processing
[/code]

My question is: how many calls to "sqlite3_finalize() should be there?

Do I need one inside lines 2-6? And how to properly handle failure with
statement release?

Thank you.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error handling

Igor Tandetnik-2
On 2/15/2017 7:02 PM, Igor Korot wrote:
> My question is: how many calls to "sqlite3_finalize() should be there?

For every successful call to sqlite3_prepare[_v2], there should
eventually be a call to sqlite3_finalize; otherwise, you'd leak a
statement, and prevent the database handle from closing cleanly.

> Do I need one inside lines 2-6?

You don't. If sqlite3_prepare_v2 fails, you wouldn't have a valid handle
to pass to sqlite3_finalize.

> And how to properly handle failure with statement release?

sqlite3_finalize() itself never fails. Through a quirk of history, it
may return an error code from the most recent failed sqlite3_step() call
(see Goofy Interface Alert section at https://sqlite.org/c3ref/step.html 
). Since you are using sqlite3_prepare_v2, you may ignore the return
value of sqlite3_finalize.
--
Igor Tandetnik

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error handling

Igor Korot
Hi, Igor,


On Feb 15, 2017 7:16 PM, "Igor Tandetnik" <[hidden email]> wrote:

On 2/15/2017 7:02 PM, Igor Korot wrote:

> My question is: how many calls to "sqlite3_finalize() should be there?
>

For every successful call to sqlite3_prepare[_v2], there should eventually
be a call to sqlite3_finalize; otherwise, you'd leak a statement, and
prevent the database handle from closing cleanly.


Ok.



Do I need one inside lines 2-6?
>

You don't. If sqlite3_prepare_v2 fails, you wouldn't have a valid handle to
pass to sqlite3_finalize.


And how to properly handle failure with statement release?
>

sqlite3_finalize() itself never fails. Through a quirk of history, it may
return an error code from the most recent failed sqlite3_step() call (see
Goofy Interface Alert section at https://sqlite.org/c3ref/step.html ).
Since you are using sqlite3_prepare_v2, you may ignore the return value of
sqlite3_finalize.


Well, my question here is a bit different - if sqlite3_step () returns an
error
should the statement be released?


Thank you.


--
Igor Tandetnik

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error handling

Richard Hipp-3
On 2/15/17, Igor Korot <[hidden email]> wrote:
>
> Well, my question here is a bit different - if sqlite3_step () returns an
> error
> should the statement be released?
>

You still need to invoke either sqlite3_finalize() or sqlite3_reset()
on the statement.  Use sqlite3_finalize() if you will never use that
statement again, and sqlite3_reset() if you want to reuse it.  Just be
cause it got an error on one go doesn' t mean that it cannot be
reused, if that is what you are asking.

--
D. Richard Hipp
[hidden email]
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error handling

Igor Korot
Hi, Richard,

On Wed, Feb 15, 2017 at 8:20 PM, Richard Hipp <[hidden email]> wrote:

> On 2/15/17, Igor Korot <[hidden email]> wrote:
>>
>> Well, my question here is a bit different - if sqlite3_step () returns an
>> error
>> should the statement be released?
>>
>
> You still need to invoke either sqlite3_finalize() or sqlite3_reset()
> on the statement.  Use sqlite3_finalize() if you will never use that
> statement again, and sqlite3_reset() if you want to reuse it.  Just be
> cause it got an error on one go doesn' t mean that it cannot be
> reused, if that is what you are asking.

Basically I was asking if the failure on the sqlite3_step() release the
statement handle or not. So thank you for confirming it is not.

Now I presume that calling sqlite3_finalize() on the NULL handle is safe?
Because I am planning to call this function only once - after the loop.

Thank you.

>
> --
> D. Richard Hipp
> [hidden email]
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error handling

Igor Tandetnik-2
On 2/15/2017 10:42 PM, Igor Korot wrote:
> Now I presume that calling sqlite3_finalize() on the NULL handle is safe?

Yes; the documentation explicitly states that.
--
Igor Tandetnik

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Loading...