int res = sqlite3_close( m_db );
    if( res == SQLITE_OK )
        m_db = NULL;
    else
    {
// error handling
    }
#ifdef DEBUG
    sqlite3_stmt *statement = sqlite3_next_stmt( m_db, NULL );
    if( statement )
        const char *query = sqlite3_sql( statement );
#endif

Then you simply do:

if (!m_db)
{
// the pointer does not point to anything error handling
}

or conversely:

if (m_db)
{
// do stuff with the connection
}
else
{
// database is not open error
}


> -----Original Message-----
> From: sqlite-users [mailto:[email protected]]
> On Behalf Of Igor Korot
> Sent: Tuesday, 13 December, 2016 17:55
> To: Discussion of SQLite Database; General Discussion of SQLite Database
> Subject: [sqlite] How do I check if the DB is open?
> 
> Hi, ALL,
> I'm using following code to check for errors in debug mode:
> 
>     int res = sqlite3_close( m_db );
>     if( res != SQLITE_OK )
>     {
> // error handling
>     }
> #ifdef DEBUG
>     sqlite3_stmt *statement = sqlite3_next_stmt( m_db, NULL );
>     if( statement )
>         const char *query = sqlite3_sql( statement );
> #endif
> 
> However the call to sqlite3_close() make the 'm_db' pointer invalid if
> everything is successful and hence the sqlite3_next_stmt() crashes.
> 
> How do I properly check if the DB is still open?
> Or maybe I should put the code inside #ifdef....#endif before DB closing?
> 
> Thank you.
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users



_______________________________________________
sqlite-users mailing list
[email protected]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to