I had lots of problems here when starting with SQLite and painstaking I
think I've figured it out.

You have sqlite3_prepare, which compiles the sql into byte code, then 
Sqlite3_step to execute the query or update, if it is an update then
there is no row, if query then call step until no more rows. 

Once done stepping you must either sqlite3_reset or sqlite3_finalize
I believe that the finalize will do reset and free resources. Reset is
designed to reuse the query or update.

It is important though to reset (if you don't finalize) because if you
don't reset you may have an open lock on the table and this will lock
out other processes and they will get a SQLITE_BUSY error, because
depending on what the sql is doing, it may have a cursor which may lock
the table.

So your code is fine.
But at the end of your rows you don't have to call finalize but you must
call reset.
You don't have to call finalize right away but maybe on object
destruction, to free the resources, after reset is called you wont have
a lock on the table anymore.

This is how I understand things but would like for someone to tell me if
I'm wrong.


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of
anand chugh
Sent: 19 June 2007 07:28 AM
To: sqlite-users@sqlite.org
Subject: [sqlite] Step Query

Hi

I am having code like this:

   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
   if( rc!=SQLITE_OK ){
     return rc;
   }
   sqlite3_bind_text(pStmt, 1, zKey, -1, SQLITE_STATIC);
   sqlite3_bind_blob(pStmt, 2, zBlob, nBlob, SQLITE_STATIC);

   while( sqlite3_step(pStmt)==SQLITE_ROW )
 {
     *pnBlob = sqlite3_column_bytes(pStmt, 0);
     *pzBlob = (unsigned char *)malloc(*pnBlob);
     memcpy(*pzBlob, sqlite3_column_blob(pStmt, 0), *pnBlob);
   }

  sqlite3_finalize(pStmt);

My question here is do I need to do sqlite3_finalize(pStmt); after
every sqlite3_step() to free all memory allocated by
sqlite3_step().Does calling finalize at end will free all memory
allocated by all steps statements?

 Example shown http://www.sqlite.org/cvstrac/wiki?p=BlobExample does
same , it calls finalize after  every step.

My Program shows some Memory Leaks(Virtual Bytes).

Please clarify.

Anand

------------------------------------------------------------------------
-----
To unsubscribe, send email to [EMAIL PROTECTED]
------------------------------------------------------------------------
-----


-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to