sqlite3_reset()何时需要?

时间:2016-03-02 07:40:55

标签: c sqlite

在你告诉我阅读docs之前,是的,我已经阅读了它们。但是我自己的测试告诉我,我有时可以在一些准备好的语句中反复调用sqlite3_step()而不先重置它们。例如:BEGINCOMMIT。当我需要重置预先准备好的声明时,您是否可以解释一下,因为我希望尽可能避免拨打电话?

编辑:文档说(我的大胆):

  

准备好的陈述对象通常的生命周期如下:

2 个答案:

答案 0 :(得分:5)

来自sqlite来源:

/* 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.
*/

因此,对于版本3.7.0,如果您达到SQLITE_DONE并且想要再次执行,则不需要sqlite_reset。

完成后,您需要调用sqlite3_finalize来释放该语句。查看https://www.sqlite.org/c3ref/finalize.html

答案 1 :(得分:0)

我偶然发现,没有重置语句的另一个问题是,在重置准备好的语句之前,数据库可能保持锁定状态。例如。 sqlite3_wal_checkpoint()将返回SQL_LOCKED。