Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite3_reset() when is it needed?

Tags:

c

sqlite

Before you tell me to read the docs, yes I have read them. But my own tests tell me I can sometimes call repeatedly sqlite3_step() on some prepared statements without resetting them first. Examples are: BEGIN and COMMIT. Can you shed some light on when I need to reset a prepared statement, as I would like to avoid making the call whenever possible?

EDIT: The docs say (my bold):

The life-cycle of a prepared statement object usually goes like this:

like image 927
user1095108 Avatar asked Oct 29 '25 11:10

user1095108


2 Answers

From the sqlite sources:

/* We used to require that sqlite3_reset() be called before retrying
** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
** with version 3.7.0, we changed this so that sqlite3_reset() would
** be called automatically instead of throwing the SQLITE_MISUSE error.
** This "automatic-reset" change is not technically an incompatibility, 
** since any application that receives an SQLITE_MISUSE is broken by
** definition.
**
** Nevertheless, some published applications that were originally written
** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 
** returns, and those were broken by the automatic-reset change.  As a
** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
** legacy behavior of returning SQLITE_MISUSE for cases where the 
** previous sqlite3_step() returned something other than a SQLITE_LOCKED
** or SQLITE_BUSY error.
*/

So with version 3.7.0 sqlite_reset is not needed if you reach SQLITE_DONE and want to step through again.

You need to call sqlite3_finalize to free the statement when you are done with it.see https://www.sqlite.org/c3ref/finalize.html

like image 108
frast Avatar answered Oct 31 '25 00:10

frast


One more issue with not resetting statement, that I stumbled upon, is that DB may remain locked until the prepared statement is reset. E.g. sqlite3_wal_checkpoint() would return SQL_LOCKED.

like image 41
Alex Che Avatar answered Oct 31 '25 02:10

Alex Che