How to get soci::rowset<std::string> in sql query

290 Views Asked by At

I have a table with a column name and age.

And I am trying to get all name as

soci::session& sql_session(conn->Session());
soci::rowset<std::string> rs = sql_session.prepare << select name from Person;
std::vector<std::string> plist;
std::copy(rs.begin(), rs.end(), plist.begin());

But my program crashes at std::copy. Seems like both the containers(source and destination) are not same for std::copy. can we do this using ORM, how?

1

There are 1 best solutions below

0
On

If you need to read only strings it is not to use rowset, in that case you don't need to copy:

const size_t namesLimit = 100;
std::vector<std::string> plist(namesLimit);
soci::session sql_session(...); // connection details
sql_session << "select name from Person;", into(plist);

Of course if you need to get all names without hardcoding limit you can use earlier:

size_t namesLimit ;
sql_session << "select count(*) from Person", into(namesLimit);