Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite3 preparing, binding and resetting statements

Can someone put more light on the functions:

sqlite3_reset();
sqlite3_clear_bindings()  

I understand that I can use sqlite3_prepare() to translate sql string to native byte code which engine understands. Therefor the engine does not have to translate it every time when it is used. I can also parametrize such prepared statement and later bind values to it with sqlite3_bind*() functions.

I can assign NULL value to these binded params with sqlite3_clear_bindings(). From documentation one can read that sqlite3_reset(), does not destroy bindings, the values are preserved and the object is put into initial state. What does it actually mean? Especially the part about not destroying bindings.

like image 467
name Avatar asked Sep 29 '10 13:09

name


People also ask

Does sqlite support prepared statements?

All SQL must be converted into a prepared statement before it can be run. The life-cycle of a prepared statement object usually goes like this: Create the prepared statement object using sqlite3_prepare_v2(). Bind values to parameters using the sqlite3_bind_*() interfaces.

What is sqlite3_prepare_v2?

The sqlite3_prepare_v2() function takes five parameters. The first parameter is the database handle obtained from the sqlite3_open() function. The second parameter is the SQL statement to be compiled. The third parameter is the maximum length of the SQL statement measured in bytes.

What is sqlite3_bind_text?

sqlite3_bind_text() is for UTF-8 strings. sqlite3_bind_text16() is for UTF-16 strings using your processor's native endianness. sqlite3_bind_text64() lets you specify a particular encoding (utf-8, native utf-16, or a particular endian utf-16). You'll probably never need it.

What is sqlite3_stmt?

The sqlite3_stmt structure holds a prepared statement. This is all the state and execution information required to build and execute an SQL statement. Statements are used to set any bound parameter values and get any result values.


1 Answers

sqlite3_reset clears the state that the prepared statement maintained during execution. This sets it back to the initial state, thus "resetting it". Bindings remain intact. The statement can be re-executed. Without resetting it, you will receive an error when you try to execute it.

sqlite3_clear_bindings will just clear the bindings, but not change the state on the prepared statement. You can't re-execute a prepared statement if you just cleared the bindings.

like image 68
NG. Avatar answered Sep 18 '22 03:09

NG.