wxSQLite3 passing database pointer support?

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
ouch67
Earned some good credits
Earned some good credits
Posts: 135
Joined: Sun Mar 23, 2008 12:09 am

wxSQLite3 passing database pointer support?

Post by ouch67 »

I'm getting problems when trying to the pointer of my already opened database to another window.

I'm just using similar code to this:

Code: Select all

wxSQLite3Database* newdb;
wxSQLite3Database olddb;

newdb = &olddb;

wxSQLite3Table ResultTable;
ResultTable = newdb->GetTable("select Name from TABLE;");
I get a crash on the GetTable line. (i've ran the sql command through the sqlite3 utility and it works so that's not it)

Is passing the pointer to a database is not allowed? Or am I just doing it wrong.
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxSQLite3 passing database pointer support?

Post by utelle »

ouch67 wrote:I'm getting problems when trying to the pointer of my already opened database to another window.
In principle this should not impose a problem. In my own application I quite often use a global wxSQLite3 database instance like in the following code snippet:

Code: Select all

wxSQLite3Database* Resources::GetDatabase()
{
  if (ms_db == NULL)
  {
    static wxSQLite3Database db;
    ms_db = &db;
  }
  return ms_db;
}
The only thing you have to keep in mind is that this database instance should not be used from different threads. If you use it exclusively from the main thread of a wxWidgets application it should work like a charm.
ouch67 wrote:I'm just using similar code to this:

Code: Select all

wxSQLite3Database* newdb;
wxSQLite3Database olddb;

newdb = &olddb;

wxSQLite3Table ResultTable;
ResultTable = newdb->GetTable("select Name from TABLE;");
I get a crash on the GetTable line. (i've ran the sql command through the sqlite3 utility and it works so that's not it)

Is passing the pointer to a database is not allowed? Or am I just doing it wrong.
From the code fragment you showed it's not clear whether your database pointer is valid or not. Also not clear is whether your application crashes due to an access violation (i.e. null pointer) or due to missing exception handling - although I would assume the latter.

wxSQLite3 throws an exception if the internal database pointer is invalid or if the table can't be fetched. Please surround your call to GetTable by a try-catch block and check/display the error message of the wxSQLite3Exception. This should give you a hint what's going wrong.

Regards,

Ulrich
ouch67
Earned some good credits
Earned some good credits
Posts: 135
Joined: Sun Mar 23, 2008 12:09 am

Re: wxSQLite3 passing database pointer support?

Post by ouch67 »

What is the best way to catch an wxSQLite3 exception?

I tried:

Code: Select all

    try
    {
        ResultTable = dbs->GetTable("select Name from TABLE;");
    }
    catch(int e)
    {
        wxMessageBox(wxSQLite3Exception::ErrorCodeAsString(e));
    }
But no error message came up so I assume nothing was thrown?
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxSQLite3 passing database pointer support?

Post by utelle »

ouch67 wrote:What is the best way to catch an wxSQLite3 exception?

I tried:

Code: Select all

    try
    {
        ResultTable = dbs->GetTable("select Name from TABLE;");
    }
    catch(int e)
    {
        wxMessageBox(wxSQLite3Exception::ErrorCodeAsString(e));
    }
But no error message came up so I assume nothing was thrown?
How is this supposed to work? Of course you have to catch not int e but wxSQLite3Exception e:

Code: Select all

  try
  {
    ResultTable = dbs->GetTable("select Name from TABLE;");
  }
  catch (wxSQLite3Exception& e)
  {
    wxMessageBox(e.GetMessage());
  }
wxSQLite3 includes a minimal sample showing such things.

Regards,

Ulrich
ouch67
Earned some good credits
Earned some good credits
Posts: 135
Joined: Sun Mar 23, 2008 12:09 am

Re: wxSQLite3 passing database pointer support?

Post by ouch67 »

Well I tried your code and I don't see a message box either, so apparently it's crashing before wxSqlite3 takes over but the crash is on that line. Hmm...
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSQLite3 passing database pointer support?

Post by doublemax »

Are you sure the pointer is valid? Maybe show some real code.
Use the source, Luke!
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxSQLite3 passing database pointer support?

Post by utelle »

ouch67 wrote:Well I tried your code and I don't see a message box either, so apparently it's crashing before wxSqlite3 takes over but the crash is on that line. Hmm...
The code of the method GetTable takes measures to not access a null pointer. The status of the database is checked internally, before calling the respective SQLite function. That is, there is no obvious reason that wxSQLite3 could crash within method GetTable.

Have you checked that your database pointer is valid before calling the GetTable method? You wouldn't see a wxSQLite3Exception thrown if the database pointer is invalid, because your application would crash on calling the method.

Another possibility would be that calling the respective SQLite function fails. This could happen if you use a SQLite DLL which does not contain the function sqlite3_get_table. Unlikely, but not impossible.

Please use a debugger to step through your application to verify that your database pointer is valid and that you can step into method GetTable and determine at which line within wxSQLite3 your application crashes, if you can step into GetTable.

Do you get any error messages when your application crashes? If yes, tell which ones.

For testing you could issue a GetTable call immediately after you opened the database, that is, before passing any pointers around. Does this work? Or does it crash, too?

Last but not least, describe your environment: operating system, wxWidgets version, compiler ...

Regards,

Ulrich
ouch67
Earned some good credits
Earned some good credits
Posts: 135
Joined: Sun Mar 23, 2008 12:09 am

Re: wxSQLite3 passing database pointer support?

Post by ouch67 »

I got it figured out. Turns out it's a good idea to make sure your passing your pointers to a valid variable... lol

Basically there was a public variable in a class that was supposed to receive the pointer but it wasn't fully initialized by the time the database pointer was sent to it.

Thanks for the help though! I think I'll sprinkle that try-catch block code throughout my program just in case.
Post Reply