开发者

How to prevent Sqlite unbound parameters interpreted as NULL

开发者 https://www.devze.com 2023-02-02 09:35 出处:网络
As the Sqlite docs specify, unbound parameters in prepared statements are interpreted as NULL - my question is this then:

As the Sqlite docs specify, unbound parameters in prepared statements are interpreted as NULL - my question is this then:

Is there a way to have Sqlite ensure that all parameters have been bound at least once, thereby ensuring none were missed by a开发者_JAVA百科ccident?

It is better to get an error and require calls to sqlite3_bind_null(statement_, col); then to get a subtle error because I forgot to call sqlite3_bind_* on the where clause of an update statement!


It is not possible to differentiate unbound parameters from parameters set to NULL using the current SQLite libraries.

If you have a look at the C source code for sqlite3_bind_null(), you will find that it simply calls the internal SQLite function that unbinds a parameter. Therefore there is no way to tell the two cases apart.

The only solution for this would be to wrap the SQLite C API functions with your own functions that will do a bit of book-keeping. You can bundle a bitmap with each sqlite3_stmt structure, with each bit set to true if and only if the corresponding parameter is bound. You would have to create wrappers for at least the sqlite3_bind_*() functions and sqlite3_clear_bindings().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号