wxsqlite3 retrieve database data and put into variable Topic is solved

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
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

Post by DarthVega7 »

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: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxsqlite3 retrieve database data and put into variable

Post by DenDev »

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

Post by DarthVega7 »

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: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxsqlite3 retrieve database data and put into variable

Post by DenDev »

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: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxsqlite3 retrieve database data and put into variable

Post by utelle »

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: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxsqlite3 retrieve database data and put into variable

Post by DenDev »

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
Post Reply