SQL / C++ Tutorial using sqlwrapped
14. Displaying many rows
PreviousIndex
The generated class can also be spawned using a Query pointer as
second input parameter.
The result from the Query class is then used to fill the fields of the
generated class.
An optional third parameter can be used as an index into the result set.
example
#include <sqlite3.h>
#include <libsqlitewrapped.h>
#include <libtutorialdb.h>
int main()
{
StderrLog log;
Database db("tutorial.db", &log); // sqlite3 specific
Query q(db);
q.get_result("select * from playerresource");
while (q.fetch_row())
{
db::Playerresource x(&db, &q);
db::Player player(db, x.player);
db::Resource r(db, x.resource);
printf("%s owns %ld piece%s of %s.\n",
player.name.c_str(),
x.amount,
(x.amount == 1) ? "" : "s",
r.name.c_str());
}
q.free_result();
}
example10_mysql.cppexample10_sqlite3.cpp
example using result index
#include <sqlite3.h>
#include <libsqlitewrapped.h>
#include <libtutorialdb.h>
int main()
{
StderrLog log;
Database db("tutorial.db", &log); // sqlite3 specific
Query q(db);
q.get_result(
"select player.*,resource.*,x.* from playerresource as x inner join player on "
"x.player=player.num inner join resource on x.resource=resource.num");
while (q.fetch_row())
{
db::Player player(&db, &q);
db::Resource r(&db, &q, player.num_cols());
db::Playerresource x(&db, &q, player.num_cols() + r.num_cols());
printf("%s owns %ld piece%s of %s.\n",
player.name.c_str(),
x.amount,
(x.amount == 1) ? "" : "s",
r.name.c_str());
}
q.free_result();
}
example11_mysql.cppexample11_sqlite3.cpp
Previous