Returning wxSQLite3ResultSet  [SOLVED]

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
paulcrown
In need of some credit
In need of some credit
Posts: 8
Joined: Mon Jul 22, 2013 6:59 pm

Returning wxSQLite3ResultSet

Postby paulcrown » Wed Jul 24, 2013 6:25 pm

Greetings,

I am sure that I am missing something simple, and since this forum has been so helpful in the past, I will ask. I am returning a wxSQLite3ResultSet, but no values are passed.

In the following sample, GetID("Jones") results in:
myres 3
res 0

Code: Select all

wxSQLite3ResultSet SQLSelect(wxString sQuery, wxArrayString& params)
{
    if(data.IsOpen())  {
        try {
            wxSQLite3Statement stmt = data.PrepareStatement(sQuery);
            int pcount = params.GetCount();
            for (int ploop = 0; ploop<pcount; ploop++) {
              stmt.Bind(ploop+1,params[ploop]);
            }
            wxSQLite3ResultSet myres = stmt.ExecuteQuery();

       unsigned int id = myres.GetInt("myid");
       cout << "myres " << id << "\n";

            stmt.Reset();
            return myres;
        }
        catch (wxSQLite3Exception& e) {
            ErrorLog(TowxString(e.GetErrorCode()) + ":" + TowxString(e.GetMessage()));
        }
    }

    return wxSQLite3ResultSet();
}

unsigned int GetID(wxString genre)
{
    wxArrayString q ;
    q.set(ToStdString(genre));
    wxSQLite3ResultSet res = SQLSelect("SELECT myid FROM mytable WHERE lastname=?;", q);
   
    unsigned int id = res.GetInt("myid");
    cout << "res " << id << "\n";
   
    return id;
}


Am I missing something required to pass a wxSQLite3ResultSet?

Thanks in advance.

utelle
Moderator
Moderator
Posts: 784
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: Returning wxSQLite3ResultSet  [SOLVED]

Postby utelle » Thu Jul 25, 2013 5:49 am

paulcrown wrote:I am sure that I am missing something simple, and since this forum has been so helpful in the past, I will ask. I am returning a wxSQLite3ResultSet, but no values are passed.

In the following sample, GetID("Jones") results in:
myres 3
res 0

Code: Select all

wxSQLite3ResultSet SQLSelect(wxString sQuery, wxArrayString& params)
{
    if(data.IsOpen())  {
        try {
            wxSQLite3Statement stmt = data.PrepareStatement(sQuery);
            int pcount = params.GetCount();
            for (int ploop = 0; ploop<pcount; ploop++) {
              stmt.Bind(ploop+1,params[ploop]);
            }
            wxSQLite3ResultSet myres = stmt.ExecuteQuery();

       unsigned int id = myres.GetInt("myid");
       cout << "myres " << id << "\n";

            stmt.Reset();
            return myres;
        }
        catch (wxSQLite3Exception& e) {
            ErrorLog(TowxString(e.GetErrorCode()) + ":" + TowxString(e.GetMessage()));
        }
    }

    return wxSQLite3ResultSet();
}

unsigned int GetID(wxString genre)
{
    wxArrayString q ;
    q.set(ToStdString(genre));
    wxSQLite3ResultSet res = SQLSelect("SELECT myid FROM mytable WHERE lastname=?;", q);
   
    unsigned int id = res.GetInt("myid");
    cout << "res " << id << "\n";
   
    return id;
}


Am I missing something required to pass a wxSQLite3ResultSet?

You should not call method Reset, before having processed the result set. Calling Reset implicitly affects the result set.

By the way, a result set represents a cursor which is positioned in front of the first row of the result set. That is, you have to call method Next of the result set before accessing the data with the Get... methods. If the result set is empty method Next will return false. If you do not call Next you may get unpredictable results.

Regards,

Ulrich

paulcrown
In need of some credit
In need of some credit
Posts: 8
Joined: Mon Jul 22, 2013 6:59 pm

Re: Returning wxSQLite3ResultSet

Postby paulcrown » Thu Jul 25, 2013 6:31 pm

As always Ulrich, you are a great help. :D

To summarize my change, for others to review; I find examples to be very helpful when first learning new concepts.

Code: Select all

wxSQLite3ResultSet SQLSelect(wxString sQuery, wxArrayString& params)
{
    if(data.IsOpen())  {
        try {
            wxSQLite3Statement stmt = data.PrepareStatement(sQuery);
            int pcount = params.GetCount();
            for (int ploop = 0; ploop<pcount; ploop++) {
              stmt.Bind(ploop+1,params[ploop]);
            }
            wxSQLite3ResultSet myres = stmt.ExecuteQuery();
            //stmt.Reset();    // Don't reset
            return myres;
        }
        catch (wxSQLite3Exception& e) {
            ErrorLog(TowxString(e.GetErrorCode()) + ":" + TowxString(e.GetMessage()));
        }
    }
    return wxSQLite3ResultSet();
}

unsigned int GetID(wxString genre)
{
    wxArrayString q ;
    q.set(ToStdString(genre));
    wxSQLite3ResultSet res = SQLSelect("SELECT myid FROM mytable WHERE lastname=?;", q);
   
    int id;
    if (myres.NextRow()) {
           id  = res.GetInt("myid");
           cout << "Debug res " << id << "\n";
    } else {
           id = -1;
           cout << "Debug res - Not Found\n";
    }
    return id;
}


Return to “wxCode”

Who is online

Users browsing this forum: No registered users and 1 guest