I face a very strange problem today. I use some code to connect to the database I create.I create a very simple code to replace a row of data in my database.But it doesn’t work.It is strange that it can connect to the database, it can get a row of data, but it can not insert a row of data. It took me two hours to read my code, try to find out what’s wrong with my code.Finally, I found that it is because of a statement which I did not finalize .
Although I use the code to connect and operate the database, I do not have a further understanding about the code I use, which results the strange problem I faced today.
Thus, I search on the Internet and found something about the sqlite3_finalize
Destroy A Prepared Statement Object
int sqlite3_finalize(sqlite3_stmt *pStmt);
The sqlite3_finalize() function is called to delete a prepared statement. If the most recent evaluation of the statement encountered no errors or if the statement is never been evaluated, then sqlite3_finalize() returns SQLITE_OK. If the most recent evaluation of statement S failed, then sqlite3_finalize(S) returns the appropriateerror code or extended error code.
The sqlite3_finalize(S) routine can be called at any point during the life cycle of prepared statement S: before statement S is ever evaluated, after one or more calls to sqlite3_reset(), or after any call to sqlite3_step() regardless of whether or not the statement has completed execution.
Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
The application must finalize every prepared statement in order to avoid resource leaks. It is a grievous error for the application to try to use a prepared statement after it has been finalized. Any use of a prepared statement after it has been finalized can result in undefined and undesirable behavior such as segfaults and heap corruption.
From the above text which is from development document, we know that we need to finalize every statement we use.
In my original code, I just return the sqlite3_column_int(statement,0), which means that I do not finalize the statement.
Thus, I change it to:
The problem is solved.
The document about the operation of the database is in : Press here.