wxsqlite3 retrieve database data and put into variable  [SOLVED]

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
DarthVega7
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Feb 21, 2015 6:29 pm

wxsqlite3 retrieve database data and put into variable

Postby DarthVega7 » Sat Apr 18, 2015 1:06 pm

Hello,

I'm fairly new to wxsqlite3 and all I know how to do right now is execute a command:

mydb->ExecuteQuery("select * from dbtable WHERE UserID=DarthVega7")

But how do I actually retrieve the information and put it into a variable? I tried this:

string variable = mydb->ExecuteQuery("select * from dbtable WHERE UserID=DarthVega7");

Thanks,

- DV

DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 218
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxsqlite3 retrieve database data and put into variable

Postby DenDev » Sat Apr 18, 2015 1:28 pm

ExecuteQuery returns a ResultSet object that can be enummerated:

http://wxcode.sourceforge.net/docs/wxsq ... c23512930d
I have a bad habbit of not testing the code I post :D

DarthVega7
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Feb 21, 2015 6:29 pm

Re: wxsqlite3 retrieve database data and put into variable

Postby DarthVega7 » Sat Apr 18, 2015 1:52 pm

DenDev wrote:ExecuteQuery returns a ResultSet object that can be enummerated:

http://wxcode.sourceforge.net/docs/wxsq ... c23512930d



Sorry, I'm such a newb... How do I enumerate the ResultSet object?

DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 218
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxsqlite3 retrieve database data and put into variable

Postby DenDev » Sat Apr 18, 2015 2:17 pm

If your table has a column named "UserName" you could print a list of names using:

Code: Select all

wxSQLite3ResultSet rset = mydb->ExecuteQuery("select UserName from SomeTable");

while (!rset.Eof())
{

   cout << rset.GetString("UserName") << endl;
   rset.NextRow();
   
}
I have a bad habbit of not testing the code I post :D

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

Re: wxsqlite3 retrieve database data and put into variable  [SOLVED]

Postby utelle » Sat Apr 18, 2015 5:09 pm

DenDev wrote:If your table has a column named "UserName" you could print a list of names using:

Code: Select all

wxSQLite3ResultSet rset = mydb->ExecuteQuery("select UserName from SomeTable");

while (!rset.Eof())
{
   cout << rset.GetString("UserName") << endl;
   rset.NextRow();
}

Sorry, the above code will not work as expected. You have to call NextRow before accessing the columns of a retrieved row. In the wxSQLite3 documentation you will find for method wxSQLite3ResultSet::NextRow the description

Advances the cursor to the next row. On creation of the result set the cursor is positioned BEFORE the first row, i.e. the first call to this method makes the first row available for processing.


So the code should be similar to the following snippet:

Code: Select all

int numid;
wxString name;
wxSQLite3ResultSet query = mydb->ExecuteQuery("select numid, name from sometable;");
while (query.NextRow())
{
  numid = query.GetInt(0);
  name = query.GetString(1);
  // ... do something useful with the retrieved data
}

BTW, the wxSQLite3 distribution contains a sample which shows almost all concepts of wxSQLite3 like retrieving a resultset and iterating through a resultset or binding variables.

Regards,

Ulrich

DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 218
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxsqlite3 retrieve database data and put into variable

Postby DenDev » Sun Apr 19, 2015 12:56 pm

utelle wrote:Sorry, the above code will not work as expected. You have to call NextRow before accessing the columns of a retrieved row.


Thank you for clarifying that :)
I have a bad habbit of not testing the code I post :D


Return to “wxCode”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest