Page 1 of 1
wxsqlite3 retrieve database data and put into variable
Posted: Sat Apr 18, 2015 1:06 pm
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
Re: wxsqlite3 retrieve database data and put into variable
Posted: Sat Apr 18, 2015 1:28 pm
by DenDev
ExecuteQuery returns a ResultSet object that can be enummerated:
http://wxcode.sourceforge.net/docs/wxsq ... c23512930d
Re: wxsqlite3 retrieve database data and put into variable
Posted: Sat Apr 18, 2015 1:52 pm
by DarthVega7
Sorry, I'm such a newb... How do I enumerate the ResultSet object?
Re: wxsqlite3 retrieve database data and put into variable
Posted: Sat Apr 18, 2015 2:17 pm
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();
}
Re: wxsqlite3 retrieve database data and put into variable
Posted: Sat Apr 18, 2015 5:09 pm
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
Re: wxsqlite3 retrieve database data and put into variable
Posted: Sun Apr 19, 2015 12:56 pm
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