When to do a statement completion in sqlite in iOS?

When using sqlite in iOS, when to do an expression of completion?

Do I need to complete the statement after each request?

What is the difference between reset and finalize?

If I do a reset, do I need to finalize?

Thanks.

+7
source share
1 answer

You use the sqlite3_finalize() function when you are completely done with the statement, or because you made a one-time query, as shown below:

 const char *sql = "SELECT COUNT(*) FROM bonds WHERE molecule=? AND structure=?"; sqlite3_stmt *bondCountingStatement; unsigned int totalBondCount = 0; if (sqlite3_prepare_v2(database, sql, -1, &bondCountingStatement, NULL) == SQLITE_OK) { sqlite3_bind_int(bondCountingStatement, 1, databaseKey); sqlite3_bind_int(bondCountingStatement, 2, numberOfStructureBeingDisplayed); if (sqlite3_step(bondCountingStatement) == SQLITE_ROW) { totalBondCount = sqlite3_column_int(bondCountingStatement, 0); } else { } } sqlite3_finalize(bondCountingStatement); 

or if you are done with a prepared expression that you no longer need (possibly because you are removing the application).

If you created a statement that you want to use again and again (for performance reasons, since it is quite expensive to prepare a report), you use sqlite3_reset() to reset the query so that it is ready for use again. You do not want to exit in this case until you decide that you will never want to reuse this operator (again, this usually happens when your application or specific object breaks).

An example of a query that only resets the statement at the end is as follows:

 sqlite3_bind_text(insertMoleculeSQLStatement, 1, [filename UTF8String], -1, SQLITE_TRANSIENT); int success = sqlite3_step(insertMoleculeSQLStatement); // Because we want to reuse the statement, we reset it instead of finalizing it. sqlite3_reset(insertMoleculeSQLStatement); 
+11
source

All Articles