Jay Sprenkle wrote:
> Here's some example code:
>
> sqlite3*        db;
>
> // connect to database
> if ( sqlite3_open( "test.db", &db ) )
>   throw "Can't open database";
>
> char* sql;
>
> // two forms of the same sql
> sql = "SELECT one.test1, two.test2"
>      " FROM one"
>      " INNER JOIN two ON one.id = two.id"
>      ;
> sqlite3_stmt*   pStmt;
>
> if ( sqlite3_prepare( db, sql, strlen(sql), &pStmt, NULL ) != SQLITE_OK )
>   {
>      string str = "Cannot prepare sql: ";
>      str += sql[t];
>      str += ", Error: ";
>      str += sqlite3_errmsg(db);
>      throw str.c_str();
>   }
>
> bool Loop = true;
> while ( Loop )
>   switch ( sqlite3_step( pStmt ) )
>      {
>         case SQLITE_ROW:
>            // retrieve the results
>            char* p = (char *) sqlite3_column_text( pStmt, 0 );
>            string test1  = string( p ? p : "" );
>
>            p = (char *) sqlite3_column_text( pStmt, 1 );
>            string test2 = string( p ? p : "" );
>
>            break;
>         case SQLITE_DONE:
>            Loop = false;
>            break;
>         case SQLITE_BUSY:
>         case SQLITE_LOCKED:
>         default:
>            string str = "Cannot execute sql: ";
>            str += sql[t];
>            str += ", Error: ";
>            str += sqlite3_errmsg(db);
>            throw str.c_str();
>            break;
>      }
>
> // clean up when finished
> sqlite3_finalize( pStmt );
>
> sqlite3_close( db );

Jay, thank you very much man! That answers a lot. And it showed me that
I was not checking the SQLITE_LOCKED case.

But, from what I can see, if your database is busy or locked you just
stop your program execution, or you will end this function WITHOUT
running neither sqlite3_finalize nor sqlite3_close. Either way you will
have a memory leak and this is not a good thing when you're running an
daemon (my case).

What if you put an loop = false instead of the throw at the 'default'
case? Do you have to you use sqlite3_interrupt before sqlite3_finalize?

Best regards,
Daniel Colchete

Reply via email to