SQLite Forum

sqlite3_prepare()
Login

sqlite3_prepare()

(1) By anonymous on 2020-12-07 14:17:38 [link] [source]

The documentation has this statement:

The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(), and sqlite3_prepare16_v3() interfaces are recommended for all new programs. The older interfaces (sqlite3_prepare() and sqlite3_prepare16()) are retained for backwards compatibility, but their use is discouraged.

Given that the recommended functions have the same syntax:

1. What are the differences among the 4 recommended functions? 
2. Should I just use sqlite3_prepare_v2() ... is it future proof?

(2) By anonymous on 2020-12-07 14:37:20 in reply to 1 [link] [source]

My reason for asking question 2 above is that ?v3() is enumerated before ?v2():

The details of the behavior of the sqlite3_step() interface depend on whether the statement was prepared using the newer "vX" interfaces sqlite3_prepare_v3(), sqlite3_prepare_v2(), sqlite3_prepare16_v3(), sqlite3_prepare16_v2() or the older legacy interfaces sqlite3_prepare() and sqlite3_prepare16().

The follows this remark:

With the "v2" interface, any of the other result codes or extended result codes might be returned as well.

(4) By Adrian Ho (lexfiend) on 2020-12-07 15:37:30 in reply to 2 [link] [source]

My reason for asking question 2 above is that ?v3() is enumerated before ?v2():

And sqlite3_prepare_v2() is enumerated before sqlite3_prepare16_v3(). I see no reason to ascribe an EOL differential based on either observation.

With the "v2" interface, any of the other result codes or extended result codes might be returned as well.

One thing about technical documentation in general: Context is key. The quote you cherry-picked from https://sqlite.org/c3ref/step.html actually states, in full:

In the legacy interface, the return value will be either SQLITE_BUSY, SQLITE_DONE, SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE. With the "v2" interface, any of the other result codes or extended result codes might be returned as well.

which clearly contrasts the legacy interface with "v2", rather than v2" with "v3". (Though I guess a case could be made for adding "v3" to the latter statement for clarity.)

(3) By Adrian Ho (lexfiend) on 2020-12-07 15:27:31 in reply to 1 [source]

  1. What are the differences among the 4 recommended functions?

As the page you quoted says:

The second argument, "zSql", is the statement to be compiled, encoded as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(), and sqlite3_prepare_v3() interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(), and sqlite3_prepare16_v3() use UTF-16.

[...]

sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having the extra prepFlags parameter, which is a bit array consisting of zero or more of the SQLITE_PREPARE_* flags. The sqlite3_prepare_v2() interface works exactly the same as sqlite3_prepare_v3() with a zero prepFlags parameter.

So, in a nutshell, UTF-8 vs UTF-16, and prepare flags or lack thereof.

  1. Should I just use sqlite3_prepare_v2() ... is it future proof?

It's about as future proof as anything in SQLite, I guess.

(5) By Richard Hipp (drh) on 2020-12-07 15:47:30 in reply to 1 [link] [source]

The reason for the _v2 and _v3 interfaces is because we wanted to add new features without breaking legacy compatibility. All of these interfaces are future proof. If we need to make additional enhancements, there will be a _v4.