I don't understand why is the code below doesn't work. I have tried and tried but nothing works. Would you guys help me spot error?
Code: Select all
wxSQLite3Statement stmt = db->PrepareStatement("SELECT Book, Chapter FROM Encylopedia WHERE Book=? AND Chapter=?;");
wxSQLite3ResultSet rs =stmt.ExecuteQuery();
Strange enough replacing prepared statement with code below works, which make me suspect there is something wrong with my prepared statement code, but cannot just spot it
Code: Select all
wxSQLite3ResultSet rs = db->ExecuteQuery(wxT("SELECT * FROM Encylopedia WHERE Book=1 AND Chapter=1;"));
In the first case the wxSQLite3Statement instance stmt
owns the underlying SQLite statement object and the ownership is not tranferred to the result set. Since stmt
is a local variable it's destructor is called when it goes out of scope. Thus the underlying SQLite statement object is finalized and the result set returned by ExecuteQuery isn't valid any longer. In the second case the result set owns the underlying SQLite statement object, and your function works as you expect.
Currently the result set returned by a prepared statement needs to be processed within the scope of the prepared statement. Probably I should consider to provide a way to pass forward the ownership of the underlying SQLite statement object to the result set. But this would have it's drawbacks since a prepared statement is often used to process the same query over and over again with different parameters and that wouldn't work reliably anymore if the ownership of the SQLite statement object is given away to a result set.
For the time being as a workaround you would need to pass the wxSQLite3Statement instance holding the prepared statement as a parameter to your function, so that it doesn't go out of scope when the function returns the result set. Certainly not a very elegant solution but currently the only one to make your code work.