简体   繁体   English

如何获取 soci::rowset<std::string> 在 sql 查询</std::string>

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

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.但是我的程序在 std::copy 处崩溃。 Seems like both the containers(source and destination) are not same for std::copy.似乎容器(源和目标)对于 std::copy 都不相同。 can we do this using ORM, how?我们可以使用 ORM 来做到这一点,如何?

If you need to read only strings it is not to use rowset , in that case you don't need to copy:如果您只需要读取字符串,则不要使用rowset ,在这种情况下您不需要复制:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM