SQLite Forum

Confused about sqlite3_step / sqlite_reset / sqlite3_bind_xxx APIs
Login
Hi all,

I'm running into SQLITE_MISUSE issues and I guess I'm really missing something.

So, the first thing I do in my application is preparing all required statements, I'm using the sqlite3_prepare_v2() API, and I read the "Goofy Interface Alert" in the documentation.

For SELECT queries, I first sqlite3_bind_xxx() all parameters and then loop using sqlite3_step(). When sqlite3_step() returns SQLITE_ROW, I know I have a row from which I can extract column values, and when sqlite3_step() returns SQLITE_DONE I know I processed each row.

For INSERT/UPDATE/DELETE queries, I do the same, but call sqlite3_step() only once, which will either returns SQLITE_DONE or an error (like a constraint violation).

I read in the documentation that since 3.6.23.1, which I'm well above, I do not need to manually call sqlite3_reset() if the sqlite3_step() returned anything other than SQLITE_ROW. And I understood this as "you only need to use sqlite3_reset() when you found the row you needed and won't go further in the cursor".

However, I'm discovering that when trying to reuse the statement with new bindings, sqlite3_bind_xxx() fails with SQLITE_MISUSE. This is indeed documented, but so the "automatic sqlite3_reset()" only works when reexecuting the exact same query, with the exact same bindings, is this correct?

I'm also confused about the value returned by sqlite3_reset() when the previous sqlite3_step() returned something else than SQLITE_DONE or SQLITE_ROW (any error in other words). Will sqlite3_reset() just return the same error as the sqlite3_step()? Why isn't it simply returning SQLITE_OK in this scenario?

Thanks in advance for your enlightenment.

Best regards,

Etienne