Re: [sqlite] How do I check if the DB is open?
> On Dec 13, 2016, at 8:01 PM, Igor Korot wrote: > > Yes, you are correct. > Do you know how I can write such a code? Just move the code that looks for statements into the " if( res != SQLITE_OK )” block. —Jens ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
Perhaps you should change your calling sequence so that you call sqlite3_next_stmt() BEFORE sqlite3_close()? If you exclusively use sqlte3_prepare_v2() you can retrieve the text of the unfinalized statement(s), print/log that, and maybe even call sqlite3_finalize() if you like. -Ursprüngliche Nachricht- Von: sqlite-users [mailto:sqlite-users-boun...@mailinglists.sqlite.org] Im Auftrag von Igor Korot Gesendet: Mittwoch, 14. Dezember 2016 01:55 An: Discussion of SQLite Database ; General Discussion of SQLite Database Betreff: [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 sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ Gunter Hick Software Engineer Scientific Games International GmbH FN 157284 a, HG Wien Klitschgasse 2-4, A-1130 Vienna, Austria Tel: +43 1 80100 0 E-Mail: h...@scigames.at This communication (including any attachments) is intended for the use of the intended recipient(s) only and may contain information that is confidential, privileged or legally protected. Any unauthorized use or dissemination of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the sender by return e-mail message and delete all copies of the original communication. Thank you for your cooperation. ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
On 12/14/2016 10:56 AM, Igor Korot wrote: Keith, On Tue, Dec 13, 2016 at 8:34 PM, Keith Medcalf wrote: 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 Is it OK to pass NULL as a first parameter to sqlite3_next_stmt()? Upon checking the documentation it looks like the call will fail if m_db is NULL. Why run the sqlite3_next_stmt() call if sqlite3_close() returns SQLITE_OK. In that case you already know there are no unfinalized statements. i.e. can you not put the sqlite3_next_stmt() checks in the "// error handling" block above? Dan. Thank you. 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:sqlite-users-boun...@mailinglists.sqlite.org] 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 sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
Jens, On Tue, Dec 13, 2016 at 8:38 PM, Jens Alfke wrote: > >> On Dec 13, 2016, at 5:33 PM, Simon Slavin wrote: >> >> The only thing you should do if sqlite3_close() doesn’t work is to print an >> error message which includes the value returned. Because if you can’t close >> the database what are you going to do instead ? > > Igor is attempting to log info about any active statements that are > preventing the db from closing, for debugging purposes. > > I’ve done similar things in the past; I’ve sometimes had issues in my own > code that prevented statements from getting freed or reset after a query. Yes, you are correct. Do you know how I can write such a code? Thank you. > > —Jens > ___ > sqlite-users mailing list > sqlite-users@mailinglists.sqlite.org > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
Nathan, On Tue, Dec 13, 2016 at 8:36 PM, Nathan Bossett wrote: > On Tue, Dec 13, 2016 at 08:20:44PM -0500, Igor Korot wrote: >> > What more are you trying to find out? >> >> If I forget to finalize statement, I can use that sequence to find >> which query is dangling. >> >> So are you saying that this code should be executed if sqlite3_close() >> didn't return SQLITE_OK? > > I'm saying that your code should be trying to shut down gracefully > before calling close: finalize statements and whatever else. I understand that. That's why I'm trying to find out if everything is OK... > > If I get to a point in my code where I know I haven't done that > because the sqlite3_close() fails, then I don't have too many > intelligent options left other than to note the problem. > > So I'm not sure exactly why you're trying to create an additional > statement: once you know the close succeeded or failed, what more > are you trying to learn? If all you want to know is that bare fact > further down in the code, create a bool db_is_open or set the db > pointer. Because if graceful closure is not possible because of me I want to know right away so that I can fix the problem. Thank you. > > -Nathan > ___ > sqlite-users mailing list > sqlite-users@mailinglists.sqlite.org > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
Keith, On Tue, Dec 13, 2016 at 8:34 PM, Keith Medcalf wrote: > > 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 Is it OK to pass NULL as a first parameter to sqlite3_next_stmt()? Upon checking the documentation it looks like the call will fail if m_db is NULL. Thank you. > > 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:sqlite-users-boun...@mailinglists.sqlite.org] >> 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 >> sqlite-users@mailinglists.sqlite.org >> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users > > > > ___ > sqlite-users mailing list > sqlite-users@mailinglists.sqlite.org > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
Simon, On Tue, Dec 13, 2016 at 8:33 PM, Simon Slavin wrote: > > On 14 Dec 2016, at 1:20am, Igor Korot wrote: > >> So are you saying that this code should be executed if sqlite3_close() >> didn't return SQLITE_OK? > > Hold on. Closing the database is a special case. > > The only thing you should do if sqlite3_close() doesn’t work is to print an > error message which includes the value returned. Because if you can’t close > the database what are you going to do instead ? That's correct and that's what I am doing. > > Presumably if your program is closing the database it’s about to quit. So > just print an error message (and hope the user reports it to you) then have > your program quit as it would have done anyway. Yes, I'm about to quit. But what I'm saying is I am writing a lot of queries and I may forget to call sqlite3_finalize() on one of them. And so in order to find which query (statement) is dangling I'm trying to put those 3 lines. Problem is that if everything is good - m_db is invalid and therefore the next call to access it fails. The code should work in all cases - on successful close, then the statement pointer will be NULL and if I forgot to finalize any statement(s) - then the *query pointer will point to the non-closed query. Thank you. Thank you. > > Simon. > ___ > sqlite-users mailing list > sqlite-users@mailinglists.sqlite.org > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
> On Dec 13, 2016, at 5:33 PM, Simon Slavin wrote: > > The only thing you should do if sqlite3_close() doesn’t work is to print an > error message which includes the value returned. Because if you can’t close > the database what are you going to do instead ? Igor is attempting to log info about any active statements that are preventing the db from closing, for debugging purposes. I’ve done similar things in the past; I’ve sometimes had issues in my own code that prevented statements from getting freed or reset after a query. —Jens ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
On Tue, Dec 13, 2016 at 08:20:44PM -0500, Igor Korot wrote: > > What more are you trying to find out? > > If I forget to finalize statement, I can use that sequence to find > which query is dangling. > > So are you saying that this code should be executed if sqlite3_close() > didn't return SQLITE_OK? I'm saying that your code should be trying to shut down gracefully before calling close: finalize statements and whatever else. If I get to a point in my code where I know I haven't done that because the sqlite3_close() fails, then I don't have too many intelligent options left other than to note the problem. So I'm not sure exactly why you're trying to create an additional statement: once you know the close succeeded or failed, what more are you trying to learn? If all you want to know is that bare fact further down in the code, create a bool db_is_open or set the db pointer. -Nathan ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
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:sqlite-users-boun...@mailinglists.sqlite.org] > 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 > sqlite-users@mailinglists.sqlite.org > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
On 14 Dec 2016, at 1:20am, Igor Korot wrote: > So are you saying that this code should be executed if sqlite3_close() > didn't return SQLITE_OK? Hold on. Closing the database is a special case. The only thing you should do if sqlite3_close() doesn’t work is to print an error message which includes the value returned. Because if you can’t close the database what are you going to do instead ? Presumably if your program is closing the database it’s about to quit. So just print an error message (and hope the user reports it to you) then have your program quit as it would have done anyway. Simon. ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
Hi, Nathan, On Tue, Dec 13, 2016 at 8:03 PM, Nathan Bossett wrote: > I'm not sure exactly what you're trying to do, but if sqlite3_close() > returns SQLITE_OK then it closed and if it returns SQLITE_BUSY then it's > not closed but possibly in a messy state (my selects have default handling > too but I think those are the only defined responses). If you know it > closed, then set a flag (or vice versa). > > What more are you trying to find out? If I forget to finalize statement, I can use that sequence to find which query is dangling. So are you saying that this code should be executed if sqlite3_close() didn't return SQLITE_OK? Thank you. > > -Nathan > > On Tue, Dec 13, 2016 at 07:54:34PM -0500, Igor Korot wrote: >> 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 >> sqlite-users@mailinglists.sqlite.org >> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users > ___ > sqlite-users mailing list > sqlite-users@mailinglists.sqlite.org > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How do I check if the DB is open?
I'm not sure exactly what you're trying to do, but if sqlite3_close() returns SQLITE_OK then it closed and if it returns SQLITE_BUSY then it's not closed but possibly in a messy state (my selects have default handling too but I think those are the only defined responses). If you know it closed, then set a flag (or vice versa). What more are you trying to find out? -Nathan On Tue, Dec 13, 2016 at 07:54:34PM -0500, Igor Korot wrote: > 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 > sqlite-users@mailinglists.sqlite.org > http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
[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 sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users